本文介绍了如何使 QCombobox 可扩展为分层项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些字典变量,例如:
i have a some dictionary variables such as :
{"Courses" : ["Math" , "Science" , "English"]} ,
{"Cars" : ["Ford" , "BMW" , "Mercedes"]} , etc.
我想从 qcombobox 中选择值,但首先,我需要将光标保持在 qcombobox 中的键上,然后从突出显示的键中选择值.
i want to select values from qcombobox but firstly, i need to keep the cursor at keys from qcombobox then select the value from the highlighted key.
Courses
Cars-->Ford
-->BMW
-->Mercedes
我怎么能做这样的事情?谢谢回复.
How can i do something like that?Thanks for reply.
推荐答案
您可以将 QPushButton
与 QMenu
一起使用,如下所示:
You can use a QPushButton
with a QMenu
as I show below:
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_())
这篇关于如何使 QCombobox 可扩展为分层项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!