【问题标题】:QComboBox add bold parent itemsQComboBox 添加粗体父项
【发布时间】:2019-12-17 14:49:08
【问题描述】:

我想向 pyqt5 组合框添加某种父项,以允许对以下项进行分组。如果可能的话,父母不应该选择和大胆,孩子有点缩进。

到目前为止我得到了什么:我把它们加粗了,但我不知道不可选择的选项。我可以添加 .setEnabled(False) 但这会使它们变灰。还有比在子项前面简单地添加空白更好的方法吗?

from PyQt5.QtWidgets import QWidget, QComboBox, QApplication
import PyQt5.QtGui
import sys


class Example(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()


    def initUI(self):

        combo = QComboBox(self)

        combo.addItem("option_1")
        combo.addItem("group_1")
        combo.addItem("   option_2")
        combo.addItem("   option_3")
        combo.addItem("group_2")
        combo.addItem("   option_4")
        combo.addItem("   option_5")


        font = PyQt5.QtGui.QFont()
        font.setBold(True)

        item = combo.model().item(1, 0)  # group_1 bold
        item.setFont(font)

        item = combo.model().item(4, 0)  # group_2 bold
        item.setFont(font)


        combo.currentTextChanged.connect(lambda : print(combo.currentText()) )

        self.setGeometry(100, 100, 300, 100)
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

当前代码的样子:

【问题讨论】:

    标签: python pyqt pyqt5 qcombobox


    【解决方案1】:

    您可以创建一个自定义模型来实现组的逻辑。要禁用选择,您必须使用标志。使用角色旁边的委托来建立缩进。您不应该使用 QComboBox 的 addItem 方法,而是使用模型。

    from PyQt5 import QtCore, QtGui, QtWidgets
    
    
    GroupRole = QtCore.Qt.UserRole
    
    
    class GroupDelegate(QtWidgets.QStyledItemDelegate):
        def initStyleOption(self, option, index):
            super(GroupDelegate, self).initStyleOption(option, index)
            if not index.data(GroupRole):
                option.text = "   " + option.text
    
    
    class GroupItem(QtGui.QStandardItem):
        def __init__(self, text):
            super(GroupItem, self).__init__(text)
            self.setData(True, GroupRole)
            self._number_of_childrens = 0
            font = self.font()
            font.setBold(True)
            self.setFont(font)
            self.setFlags(self.flags() & ~QtCore.Qt.ItemIsSelectable)
    
        def addChild(self, text):
            it = QtGui.QStandardItem(text)
            it.setData(False, GroupRole)
            self._number_of_childrens += 1
            self.model().insertRow(self.row() + self._number_of_childrens, it)
            return it
    
    
    class GroupComboBox(QtWidgets.QComboBox):
        def __init__(self, parent=None):
            super(GroupComboBox, self).__init__(parent)
            self.setModel(QtGui.QStandardItemModel(self))
            delegate = GroupDelegate(self)
            self.setItemDelegate(delegate)
    
        def addGroup(self, text):
            it = GroupItem(text)
            self.model().appendRow(it)
            return it
    
        def addChild(self, text):
            it = QtGui.QStandardItem(text)
            it.setData(True, GroupRole)
            self.model().appendRow(it)
    
    
    class Example(QtWidgets.QWidget):
        def __init__(self):
            super().__init__()
            self.initUI()
    
        def initUI(self):
            combo = GroupComboBox()
    
            combo.addChild("option_1")
    
            group1 = combo.addGroup("group_1")
            group1.addChild("option_2")
            group1.addChild("option_3")
    
            group2 = combo.addGroup("group_2")
            group2.addChild("option_4")
            group2.addChild("option_5")
    
            lay = QtWidgets.QVBoxLayout(self)
            lay.addWidget(combo)
    
            self.resize(160, 60)
    
    
    if __name__ == "__main__":
        import sys
    
        app = QtWidgets.QApplication(sys.argv)
        ex = Example()
        ex.show()
        sys.exit(app.exec_())
    

    【讨论】:

      猜你喜欢
      • 2010-11-27
      • 2021-04-12
      • 2020-04-10
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 2017-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多