如何从动态表格小部件连接组合框



我正在尝试创建一个表小部件,它的行数根据SpinBox的不同而不同。

我设法做到了。在表格小部件的每一行的单元格中,我都有一个组合框。

我需要为表小部件的每一行连接组合框。当选择";国家;在接下来的两个组合框中;大写";并且相应的";城市;显得

例如当选择Country"时;USA":

  • 首都:华盛顿
  • 城市:芝加哥,纽约

我放代码是为了让它更清晰。

from PyQt5 import QtCore, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
central = QtWidgets.QWidget()
self.setCentralWidget(central)
layout = QtWidgets.QHBoxLayout(central)
self.spin = QtWidgets.QSpinBox()
layout.addWidget(self.spin)
self.table = QtWidgets.QTableWidget(0, 3)
layout.addWidget(self.table)
self.table.verticalHeader().setVisible(False)
for col, label in enumerate(('Country', 'Capital', 'City')):
header = QtWidgets.QTableWidgetItem(label)
self.table.setHorizontalHeaderItem(col, header)
self.spin.setValue(self.table.rowCount())
self.spin.valueChanged.connect(self.setRowCount)
def setRowCount(self, count):
if count == self.table.rowCount():
return
# if there are too many rows, remove them
while self.table.rowCount() > count:
self.table.removeRow(self.table.rowCount() - 1)
# if rows are going to be added, create checkable items for them
while self.table.rowCount() < count:
row = self.table.rowCount()
self.table.insertRow(row)
dic = {"Country": ['Argentina', 'USA', 'Spain'], "Capital": ['Buenos Aires', 'Washington D.C.','Madrid'], "City1": ["Rosario", "Chicago", "Barcelona"],
"City2": ["Bariloche", "New York", "Osasuna"]}

for i in range(self.table.columnCount()):
if i==0:
combobox1 = QtWidgets.QComboBox()
combobox1.addItems(dic["Country"])
self.table.setCellWidget(row, i, combobox1)                    
elif i==1:
combobox2 = QtWidgets.QComboBox()
combobox2.addItems(dic["Capital"])
self.table.setCellWidget(row, i, combobox2)
elif i==2:
combobox3 = QtWidgets.QComboBox()
combobox3.addItems(dic["City1"])
combobox3.addItems(dic["City2"])
self.table.setCellWidget(row, i, combobox3)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
test = MainWindow()
test.show()
sys.exit(app.exec_())

如果您将dict和城市信息保持在相同的索引中,那么使用当前代码非常容易。但我不确定这样做是否有效。

无论如何,这里是一个工作的解决方案与您的当前代码

from functools import partial
from PyQt5 import QtCore, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
central = QtWidgets.QWidget()
self.dic = {"Country": ['Argentina', 'USA', 'Spain'], "Capital": ['Buenos Aires', 'Washington D.C.','Madrid'], "City1": ["Rosario", "Chicago", "Barcelona"],
"City2": ["Bariloche", "New York", "Osasuna"]}
self.setCentralWidget(central)
layout = QtWidgets.QHBoxLayout(central)
self.spin = QtWidgets.QSpinBox()
layout.addWidget(self.spin)
self.table = QtWidgets.QTableWidget(0, 3)
layout.addWidget(self.table)
self.table.verticalHeader().setVisible(False)
for col, label in enumerate(('Country', 'Capital', 'City')):
header = QtWidgets.QTableWidgetItem(label)
self.table.setHorizontalHeaderItem(col, header)
self.spin.setValue(self.table.rowCount())
self.spin.valueChanged.connect(self.setRowCount)
def setRowCount(self, count):
if count == self.table.rowCount():
return
# if there are too many rows, remove them
while self.table.rowCount() > count:
self.table.removeRow(self.table.rowCount() - 1)
# if rows are going to be added, create checkable items for them
while self.table.rowCount() < count:
row = self.table.rowCount()
self.table.insertRow(row)

for i in range(self.table.columnCount()):
if i==0:
combobox1 = QtWidgets.QComboBox()
combobox1.addItems(self.dic["Country"])
combobox1.currentIndexChanged.connect(partial(self.updateCity, row))
self.table.setCellWidget(row, i, combobox1)                    
elif i==1:
combobox2 = QtWidgets.QComboBox()
combobox2.addItems(self.dic["Capital"])
self.table.setCellWidget(row, i, combobox2)
elif i==2:
combobox3 = QtWidgets.QComboBox()
combobox3.addItems(self.dic["City1"])
self.table.setCellWidget(row, i, combobox3)
def updateCity(self, index):
current_index = self.sender().currentIndex()
capital_widget = self.table.cellWidget(index, 1)
city_widget = self.table.cellWidget(index, 2)
capital_widget.setCurrentIndex(current_index)
city_widget.setCurrentIndex(current_index)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
test = MainWindow()
test.show()
sys.exit(app.exec_())

相关内容

  • 没有找到相关文章

最新更新