请告诉我如何分配给下拉列表的每个元素,QTableWidget
以便当从下拉列表中选择一个元素时,会出现一个表格,其中包含所选元素的特征(稍后我将填写) .
例如,当Combobox I
我选择汽车品牌时,会出现一个我填写的表格。
我将不胜感激。
PS。我知道您不能在生成的类中编写代码,我只是作为一个工作示例发布。
from PyQt5 import QtCore, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1920, 1000)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(10, 40, 131, 20))
self.lineEdit.setInputMask("")
self.lineEdit.setText("")
self.lineEdit.setPlaceholderText("1")
self.lineEdit.textChanged.connect(self.add1)
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(10, 100, 131, 20))
self.lineEdit_2.setInputMask("")
self.lineEdit_2.setPlaceholderText("4")
self.lineEdit_2.textChanged.connect(self.add2)
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(280, 250, 119, 22))
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("1")
self.comboBox.addItem("2")
self.comboBox.addItem("3")
self.comboBox.addItem("4")
self.comboBox.addItem("5")
self.comboBox.addItem("6")
self.comboBox.addItem("7")
self.comboBox.addItem("8")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(200, 0, 1720, 1000))
Row = 35
self.row = Row
Col = 7
self.tableWidget.setRowCount(Row)
self.tableWidget.setColumnCount(Col)
self.tableWidget.setObjectName("tableWidget")
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(3, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(4, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(5, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(6, item)
self.tableWidget.horizontalHeader().setDefaultSectionSize(237)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1920, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
for column in range(Col):
for row in range(Row):
s = self.lineEdit.setText("")
item = QtWidgets.QTableWidgetItem(s)
self.tableWidget.setItem(row, column, item)
for a in range(7):
for b in range(Row):
c = self.lineEdit_2.setText("")
i1 = QtWidgets.QTableWidgetItem(c)
self.tableWidget.setItem(a, b, i1)
def add1(self, s):
for row in range(self.row):
if self.tableWidget.item(row,0).text() == s:
self.tableWidget.editItem(self.tableWidget.item(row, 0))
def add2(self, c):
itims = self.tableWidget.findItems(c, QtCore.Qt.MatchContains)
if itims:
self.tableWidget.setCurrentItem(itims[0])
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "1"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "2"))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "3"))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("MainWindow", "4"))
item = self.tableWidget.horizontalHeaderItem(4)
item.setText(_translate("MainWindow", "5"))
item = self.tableWidget.horizontalHeaderItem(5)
item.setText(_translate("MainWindow", "6"))
item = self.tableWidget.horizontalHeaderItem(6)
item.setText(_translate("MainWindow", "7"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
永远不要更改Qt 设计器生成的代码,永远。
创建另一个从相应小部件继承的类,并使用生成的类来填充它。
作为一种选择。
QStackedWidget类提供了一个小部件堆栈,其中一次只有一个小部件可见。