如何设置 a 行的 enabled 属性QComboBox
?我希望它有一些禁用和一些启用的行。
问问题
3665 次
3 回答
3
这是一个 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_())
于 2012-06-19T13:23:30.413 回答
1
基于这个答案,我们可以将 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_())
于 2015-05-01T08:42:36.007 回答
0
将一行中的每个添加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_())
于 2012-06-19T13:23:19.757 回答