【问题标题】:Pyqt5 combobox allow search but prevent addPyqt5 组合框允许搜索但阻止添加
【发布时间】:2020-05-02 04:05:20
【问题描述】:

如何使QComboBox 允许搜索但阻止用户添加新项目 我的代码:

self.products = QtWidgets.QComboBox(self.conts[self.ind])
self.products.setEditable(True)
self.products.resize(190, 30)
self.products.move(400, 20)
self.products.setStyleSheet('background:#2c3e50')
self.products.show()

self.products.setEditable(True) 不是我需要的,因为用户可以输入任何内容

如何防止用户添加新项目,只搜索当前设置的项目

【问题讨论】:

    标签: python pyqt pyqt5 qcombobox


    【解决方案1】:

    正如您指出的,您必须启用editable 属性并将insertPolicy 属性设置为QComboBox::NoInsert

    import sys
    
    from PyQt5 import QtWidgets
    
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        w = QtWidgets.QComboBox()
        w.setEditable(True)
        w.setInsertPolicy(QtWidgets.QComboBox.NoInsert)
        w.addItems(["One", "Two", "Three", "Four", "Five"])
        w.show()
        sys.exit(app.exec_())

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 2011-12-20
      • 2016-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多