我有一个带有项目的组合框,我只想显示它们而无法选择其中任何一个。我在 Qt Designer 中搜索,但找不到正确的属性。有任何想法吗 ?
问问题
1062 次
2 回答
2
QComboBox.setEditable(False)
应该这样做: http: //pyqt.sourceforge.net/Docs/PyQt4/qcombobox.html#setEditable
于 2013-08-03T11:16:26.540 回答
2
您不能在 QtDesigner 中执行此操作,您必须将currentIndexChanged
信号与一个函数连接起来,该函数将恢复用户选择的旧值:
例子:
从 PyQt4 导入 sys 导入 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_()
于 2013-08-10T17:07:50.867 回答