1

So I have figured out how to dynamically generate controls how I wanted in QDialog for question i asked here but now I am not able to set the value on QSpinBox from the dictionary below

books = {
        'Contact':['Carl Sagan', 2],
        'End of Faith':['Sam Harris', 7],
        'on Mars':['Patrick Moore', 1],

        }


def buildUi(self):
    self.gridLayout = QtGui.QGridLayout()
    self.gridLayout.setSpacing(10)
    for index, (key, values) in enumerate(self._data.iteritems()):
        getLbl = QtGui.QLabel("Get", self)
        label = QtGui.QLabel(key, self)
        chkBox = QtGui.QCheckBox(self._data[key][0], self)
        chkBox.setToolTip("Click here to get the book")
        version = QtGui.QSpinBox(self._data[key][-1], self)
        self.gridLayout.addWidget(getLbl, index, 0)
        self.gridLayout.addWidget(label, index, 1)
        self.gridLayout.addWidget(chkBox, index, 2)
        self.gridLayout.addWidget(version, index, 3)
    self.layout = QtGui.QVBoxLayout()
    self.okBtn = QtGui.QPushButton("OK")
    self.layout.addLayout(self.gridLayout)
    self.horLayout = QtGui.QHBoxLayout()
    self.horLayout.addStretch(1)
    self.horLayout.addWidget(self.okBtn)
    self.layout.addLayout(self.horLayout)
    self.setLayout(self.layout)

Here is the error message I am getting:

Traceback (most recent call last):
  File "~/Development/custom/MessageBox.py", line 58, in _launchMessageBox
    dlg = MessageBox(self._data)
  File "~/Development/custom/MessageBox.py", line 10, in __init__
    self.buildUi()
  File "~/Development/custom/MessageBox.py", line 21, in buildUi
    version = QtGui.QSpinBox(self._data[key][-1], self)
TypeError: QSpinBox(QWidget parent=None): argument 1 has unexpected type 'int'
[Finished in 4.5s]

Also how do I set the limit in QSPinBox ?

1 Answer 1

0

According to QSpinBox.__init__ documentation, it does take only parent parameter.

Replace following line:

version = QtGui.QSpinBox(self._data[key][-1], self)

with:

version = QtGui.QSpinBox(self)

If you want the initial value to be set, use setValue:

version.setValue(self._data[key][-1])
2
  • and how do i set limit in QSpinBox? Dec 21, 2013 at 7:44
  • @san, You can use setRange method or setMinumum/setMaximum methods.
    – falsetru
    Dec 21, 2013 at 7:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.