如何在Pyqt中创建只读组合框?

时间:2013-08-03 10:33:04

标签: python user-interface pyqt

我有一个包含项目的comboBox,我只是想显示它们而不能选择其中任何一个。我在Qt Designer中搜索但我找不到合适的属性。有什么想法吗?

enter image description here

2 个答案:

答案 0 :(得分:2)

QComboBox.setEditable(False)应该这样做:http://pyqt.sourceforge.net/Docs/PyQt4/qcombobox.html#setEditable

答案 1 :(得分:2)

您无法在QtDesigner中执行此操作,您必须将currentIndexChanged信号连接到一个函数,该函数将恢复用户选择的旧值:

示例:

导入系统 来自PyQt4导入QtGui,QtCore

class MainWidget(QtGui.QWidget):
    def __init__(self):
        super(MainWidget, self).__init__()
        # Create a combo and set the second item to be selected
        self.combo = QtGui.QComboBox()
        self.combo.addItems(['foo', 'bar', 'baz'])
        self.combo.setCurrentIndex(1)
        # Connect the combo currentIndexChanged signal
        self.combo.activated.connect(self.on_combo_change)
        # Setup layout
        self.layout = QtGui.QVBoxLayout()
        self.layout.addWidget(self.combo)
        self.setLayout(self.layout)

    def on_combo_change(self, index):
        # Whatever the user do, just ignore it and revert to
        # the old value.
        self.combo.setCurrentIndex(1)


app = QtGui.QApplication(sys.argv)
mw = MainWidget()
mw.show()
app.exec_()
相关问题