how to approach a QSpinbox in other widgets?

348 views Asked by At

I'm using Qt-desiger to make a GUI.
For example I have a QSpinbox in a QCombobox in a Groubbox.
How can i use SetValue for my Spinbox? how i have to "describe" the way to my Spinbox?

2

There are 2 answers

0
qurban On BEST ANSWER

Widgets inside other Widgets do not create hierarchy in Qt, it means that if you have widget1 inside widget2 you can directly access the widget1, like self.widget1 (and self.widget2)

You can create a class which will contain all your widgets on your ui form as follows:

from PyQt4 import uic
Form, Base = uic.loadUiType(r'path\to\your\uifile.ui') #get ui Form and Base class
class Window(Form, Base):
    def __init__(self, parent = None):
        #here you can access your widgets
        #self.combobox.addItem('Item')
        #self.spinbox.setValue(10)
        ...

That's all you need.

0
ekhumoro On

You first need to use pyuic to generate a python module from the designer ui file:

pyuic4 -o mainwindow_ui.py mainwindow.ui

Then import it into your application:

from PyQt4 import QtCore, QtGui
from mainwindow_ui import Ui_MainWindow

class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setupUi(self)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

This will add all the widgets from Qt Designer, and make them attributes of the MainWindow class. The attribute names will be the same as the objectName property that was set in Qt Designer. So if the QComboBox was named "comboBox", and the QSpinBox named "spinBox", they could be accessed within the MainWindow class like this:

    self.comboBox.addItems('Foo')
    self.spinBox.setValue(10)