如何使QCombobox可扩展用于分层项目?

时间:2018-10-08 13:53:50

标签: python pyqt pyqt4 pyqt5 qcombobox

我有一些字典变量,例如:

{"Courses" : ["Math" , "Science" , "English"]} , 
{"Cars" : ["Ford" , "BMW" , "Mercedes"]} , etc.

我想从qcombobox中选择值,但首先,我需要将光标保持在qcombobox中的键上,然后从突出显示的键中选择值。

Courses
Cars-->Ford
    -->BMW
    -->Mercedes

我该怎么做? 感谢您的答复。

1 个答案:

答案 0 :(得分:1)

您可以将QPushButtonQMenu一起使用,如下所示:

PyQt4:

from PyQt4 import QtCore, QtGui

data =[{
        "Courses": ["Math", "Science", "English"]
    },
    {
        "Cars": ["Ford", "BMW", "Mercedes"]
    }
]

class ComboBoxExpandable(QtGui.QPushButton):
    currentTextChanged = QtCore.pyqtSignal(str)
    pathChanged = QtCore.pyqtSignal(list)

    def setData(self, value):
        menu = QtGui.QMenu(self)
        self.setMenu(menu)
        self.append_element(value, menu)
        menu.triggered.connect(self.on_triggered)

    @QtCore.pyqtSlot(QtGui.QAction)
    def on_triggered(self, action):
        self.setText(action.text())
        path = [action.text()] 
        w = action.parentWidget()
        while w.parentWidget() and isinstance(w.parentWidget(), QtGui.QMenu):
            path.insert(0, w.title())
            w = w.parentWidget()
        self.pathChanged.emit(path)
        self.currentTextChanged.emit(self.text())

    @staticmethod
    def append_element(value, menu):
        if isinstance(value, list):
            for e in value:
                ComboBoxExpandable.append_element(e, menu)
        elif isinstance(value, dict):
            for k, v in value.items():
                ComboBoxExpandable.append_element(v, menu.addMenu(k))
        else:
            menu.addAction(value)


class Widget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)

        combo = ComboBoxExpandable()
        combo.setData(data)

        result_label = QtGui.QLabel()

        combo.currentTextChanged.connect(result_label.setText)
        combo.pathChanged.connect(print)

        lay = QtGui.QFormLayout(self)
        lay.addRow("Select: ", combo)
        lay.addRow("Result: ", result_label)


if __name__ == '__main__':
    import sys

    app = QtGui.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

PyQt5:

from PyQt5 import QtCore, QtWidgets

data =[{
        "Courses": ["Math", "Science", "English"]
    },
    {
        "Cars": ["Ford", "BMW", "Mercedes"]
    }
]

class ComboBoxExpandable(QtWidgets.QPushButton):
    currentTextChanged = QtCore.pyqtSignal(str)
    pathChanged = QtCore.pyqtSignal(list)

    def setData(self, value):
        menu = QtWidgets.QMenu(self)
        self.setMenu(menu)
        self.append_element(value, menu)
        menu.triggered.connect(self.on_triggered)

    @QtCore.pyqtSlot(QtWidgets.QAction)
    def on_triggered(self, action):
        self.setText(action.text())
        path = [action.text()] 
        w = action.parentWidget()
        while w.parentWidget() and isinstance(w.parentWidget(), QtWidgets.QMenu):
            path.insert(0, w.title())
            w = w.parentWidget()
        self.pathChanged.emit(path)
        self.currentTextChanged.emit(self.text())

    @staticmethod
    def append_element(value, menu):
        if isinstance(value, list):
            for e in value:
                ComboBoxExpandable.append_element(e, menu)
        elif isinstance(value, dict):
            for k, v in value.items():
                ComboBoxExpandable.append_element(v, menu.addMenu(k))
        else:
            menu.addAction(value)


class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)

        combo = ComboBoxExpandable()
        combo.setData(data)

        result_label = QtWidgets.QLabel()

        combo.currentTextChanged.connect(result_label.setText)
        combo.pathChanged.connect(print)

        lay = QtWidgets.QFormLayout(self)
        lay.addRow("Select: ", combo)
        lay.addRow("Result: ", result_label)


if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

enter image description here