![](/img/trans.png)
[英]How to make dynamic row and columns of qlineedit on a click of pushbutton in pyqt5?
[英]How to make dynamic combobox PYQT5
我想制作一个动态组合框,如果我在下一个组合框中选择“Gasto”选项,我想查看例如“Agua”、“Gas”等,如果选择“Financas”,则只有“ Fundos de tesouraria " 和 " Fundos deinvestimento 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:
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_())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.