所以我已经想出了如何在QDialog中动态生成控件的问题我问here但现在我无法在字典中设置QSpinBox的值 以下
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)
以下是我收到的错误消息:
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]
另外如何在QSPinBox中设置限制?
答案 0 :(得分:0)
根据QSpinBox.__init__
documentation,它只需要parent
参数。
替换以下行:
version = QtGui.QSpinBox(self._data[key][-1], self)
使用:
version = QtGui.QSpinBox(self)
如果要设置初始值,请使用setValue
:
version.setValue(self._data[key][-1])