Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyQt设置QComboBox行的已启用属性_Python_Qt_Pyqt_Python 2.7_Pyqt4 - Fatal编程技术网

Python PyQt设置QComboBox行的已启用属性

Python PyQt设置QComboBox行的已启用属性,python,qt,pyqt,python-2.7,pyqt4,Python,Qt,Pyqt,Python 2.7,Pyqt4,如何设置QComboBox行的enabled属性?我希望它有一些禁用的行和一些启用的行。将行中的每个QComboBox添加到列表中,然后通过列表设置状态 from PyQt4 import QtCore, QtGui import sys class MainWindow(QtGui.QMainWindow): def __init__(self): super(MainWindow, self).__init__() s

如何设置
QComboBox
行的enabled属性?我希望它有一些禁用的行和一些启用的行。

将行中的每个
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_())

将行中的每个
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_())

以下是QComboBox的工作示例,其中项目1和4(如列表
disable
中指定)被禁用。我举了个例子。请参见该方法的文档


以下是QComboBox的工作示例,其中项目1和4(如列表
disable
中指定)被禁用。我举了个例子。请参见该方法的文档

基于此,我们可以将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_())
基于此,我们可以将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_())

在编辑问题之前,此解决方案更有意义;)。你是说你把这个问题解释为“一排组合框”吗?我没想到。看到我对问题的编辑后,您认为什么更有可能?在编辑问题之前,此解决方案更有意义;)。你是说你把这个问题解释为“一排组合框”吗?我没想到。在看到我对这个问题的编辑后,你认为什么更有可能?这就是我要寻找的。谢谢。兼容性说明:如果您使用的是PySide而不是PyQt,请将
QtCore.QVariant(0)
替换为
0
。这正是我想要的。谢谢。兼容性说明:如果您使用的是PySide而不是PyQt,请将
QtCore.QVariant(0)
替换为
0