我怎样才能设置的一排enabled属性QComboBox
? 我希望它有一些残疾人和一些支持行。
Answer 1:
这里是一个QComboBox,其中项目1和4(如在列表中指定的一个工作示例disable
)被禁用。 我用这个例子。 还请参阅的文档使用setData方法。
from PyQt4 import QtCore, QtGui
import sys
class Foo(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
items = ['foo', 'bar', 'yib','nuz', 'pip', 'rof']
cb = QtGui.QComboBox(self)
for i in items:
cb.addItem(i)
disable = [1,4]
for i in disable:
j = cb.model().index(i,0)
cb.model().setData(j, QtCore.QVariant(0), QtCore.Qt.UserRole-1)
if __name__ == "__main__":
app = QtGui.QApplication([])
foobar = Foo()
foobar.show()
sys.exit(app.exec_())
Answer 2:
基于这个答案 ,我们可以简化Junuxx的回答:
from PyQt4 import QtGui
import sys
class Foo(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
items = ['foo', 'bar', 'yib', 'nuz', 'pip', 'rof']
cb = QtGui.QComboBox(self)
for i in items:
cb.addItem(i)
disable = [1, 4]
for i in disable:
cb.model().item(i).setEnabled(False)
if __name__ == "__main__":
app = QtGui.QApplication([])
foobar = Foo()
foobar.show()
sys.exit(app.exec_())
Answer 3:
添加每个QComboBox
从行到一个列表,然后在列表中设置工作状态。
from PyQt4 import QtCore, QtGui
import sys
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.create_combos()
def create_combos(self):
widget = QtGui.QWidget()
self.setCentralWidget(widget)
# Create combo boxes and add them to a list.
self.combo1 = QtGui.QComboBox()
self.combo2 = QtGui.QComboBox()
self.combo3 = QtGui.QComboBox()
self.combobox_row = [self.combo1, self.combo2, self.combo3]
# Create a toggle button and connect it to the toggle method.
self.button = QtGui.QPushButton('Toggle')
self.button.setCheckable(True)
self.button.setChecked(True)
self.button.toggled.connect(self.enable_combobox_row)
# Create layout.
vbox = QtGui.QVBoxLayout()
vbox.addWidget(self.combo1)
vbox.addWidget(self.combo2)
vbox.addWidget(self.combo3)
vbox.addWidget(self.button)
widget.setLayout(vbox)
def enable_combobox_row(self, enabled):
# Work through combo boxes and set the passed enabled state.
for combobox in self.combobox_row:
combobox.setEnabled(enabled)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mainwindow = MainWindow()
mainwindow.show()
sys.exit(app.exec_())
文章来源: PyQt set enabled property of a row of QComboBox