如何制作动态组合框PYQT5
- 作者: 罐装的天才
- 来源: 51数据库
- 2022-12-23
问题描述
我想制作一个动态组合框,如果我在下一个组合框中选择Gasto"选项,我想查看例如Agua"、Gas"等,如果选择Financas",则只有"Fundos de tesouraria" 和 " Fundos deinvestimento de obriga??es"
I want to make a dynamic combobox, if i choose the option "Gasto" in the next combobox, I want to see for example "Agua", "Gas" etc, and if, choose "Financas", have only " Fundos de tesouraria " and " Fundos de investimento de obriga??es"
def initUI(self):
#self.setWindowTitle(self.title)
for t in self.tipos:
self.comboBoxCategoriaGasto.addItem(t)
for i in self.tipos2:
self.comboBoxTiposGasto_2.addItem(i)
self.tipos = ["Gasto", "Fina?as"]
self.tipos2 = ["Alimenta??o", "Transporte", "água","Luz","Gás","Internet", "Faculdade", "Depósitos a prazo","Fundos de tesouraria","Fundos de investimento de obriga??es","Fundos de investimento de a??es",]
推荐答案
你必须创建一个树型模型,其中第 n 个 QComboBox 的选中项是第 (n+1) 个 QComboBox 的 rootModelIndex:
You have to create a tree type model where the selected item of the nth QComboBox is the rootModelIndex of the (n+1)-th QComboBox:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.model = QtGui.QStandardItemModel(self)
self.combo1 = QtWidgets.QComboBox()
self.combo2 = QtWidgets.QComboBox()
self.combo1.setModel(self.model)
self.combo2.setModel(self.model)
d = {
"Gasto": ["Agua", "Gas"],
"Financas": [
"Fundos de tesouraria",
"Fundos de investimento de obriga??es",
],
}
for key, options in d.items():
root_it = QtGui.QStandardItem(key)
self.model.appendRow(root_it)
for option in options:
it = QtGui.QStandardItem(option)
root_it.appendRow(it)
self.combo1.currentIndexChanged.connect(self.onCurrentIndexChanged)
self.onCurrentIndexChanged(0)
hlay = QtWidgets.QHBoxLayout(self)
hlay.addWidget(self.combo1)
hlay.addWidget(self.combo2)
@QtCore.pyqtSlot(int)
def onCurrentIndexChanged(self, index):
ix = self.model.index(index, 0, self.combo1.rootModelIndex())
self.combo2.setRootModelIndex(ix)
self.combo2.setCurrentIndex(0)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.resize(640, 120)
w.show()
sys.exit(app.exec_())
推荐阅读
热点文章
Discord.py(重写)on_member_update 无法正常工作
0
Discord.py 在 vc 中获取用户分钟数
0
discord.py 重写 |为我的命令出错
0
Discord.py rewrite 如何 DM 命令?
0
播放音频时,最后一部分被切断.如何解决这个问题?(discord.py)
0
在消息删除消息 Discord.py
0
如何使 discord.py 机器人私人/直接消息不是作者的人?
0
(Discord.py) 如何获取整个嵌入内容?
0
Discord bot 尽管获得了许可,但不能提及所有人
0
Discord.py discord.NotFound 异常
0
