PyQt sets the row property of a QComboBox
3 answers
Here is a working example of a QComboBox where items 1 and 4 (as listed disable
) are off. I used this example. See also the documentation for the setData method .
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_())
+3
source to share
Based on this answer , we can simplify Junuxx's answer:
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_())
+1
source to share
Add each QComboBox
of the lines to the list, then go to the list by setting the state.
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_())
0
source to share