|
发表于 2023-7-6 10:16:57
|
显示全部楼层
要在QTableView内嵌QComboBox实现该功能,可以创建一个自定义的委托类,并将其应用于需要显示QComboBox的列。
首先,在你的代码中添加以下引用:
- from PyQt5.QtWidgets import QStyledItemDelegate, QComboBox
复制代码
然后,创建一个自定义的委托类,继承自QStyledItemDelegate,并重写createEditor()、setEditorData()和setModelData()方法。
- class ComboBoxDelegate(QStyledItemDelegate):
- def __init__(self, parent=None):
- super().__init__(parent)
- def createEditor(self, parent, option, index):
- combobox = QComboBox(parent)
- # 在这里设置QComboBox的选项
- return combobox
- def setEditorData(self, editor, index):
- # 获取数据并设置QComboBox的选择项
- data = index.model().data(index, Qt.DisplayRole)
- combobox = editor
- combobox.setCurrentText(data)
- def setModelData(self, editor, model, index):
- # 获取QComboBox的选中项并进行保存
- combobox = editor
- data = combobox.currentText()
- model.setData(index, data, Qt.EditRole)
复制代码
接下来,在你的代码中使用自定义的委托类。
- # 创建委托对象
- delegate = ComboBoxDelegate(self.tableView)
- # 将委托对象应用于需要显示QComboBox的列(假设是第2列,索引为1)
- self.tableView.setItemDelegateForColumn(1, delegate)
复制代码
最后,在createEditor()方法中设置QComboBox的选项。你可以根据你的需求从数据库中获取数据,然后将其添加到QComboBox中。
- def createEditor(self, parent, option, index):
- combobox = QComboBox(parent)
- # 获取与价格对应的类型列表
- price = index.sibling(index.row(), 0).data()
- sqlprice = "SELECT C.CLINIC_TYPE FROM (SELECT A.CLINIC_TYPE,SUM(B.PRICE) AS PRICE " \
- "FROM CLINIC_TYPE_SETTING A,CURRENT_PRICE_LIST B " \
- "WHERE A.PRICE_ITEM=B.ITEM_CODE GROUP BY A.CLINIC_TYPE) C " \
- "WHERE C.PRICE={price}".format(price=price)
- Dbconnect.cursor.execute(sqlprice)
- sqlprice_result = Dbconnect.cursor.fetchall()
- # 将类型列表添加到QComboBox中
- for item in sqlprice_result:
- combobox.addItem(item[0])
- return combobox
复制代码
通过以上步骤,你可以在QTableView中实现嵌入QComboBox的功能。当用户在"price"列输入价格时,"type"列将显示与价格对应的QComboBox,并且选择项会保存到数据模型中。
以下是一个完整的示例,展示了如何在QTableView内嵌QComboBox实现该功能:
- from PyQt5.QtWidgets import QApplication, QMainWindow, QStyledItemDelegate, QComboBox, QTableView, QMessageBox
- from PyQt5.QtCore import Qt
- class ComboBoxDelegate(QStyledItemDelegate):
- def __init__(self, parent=None):
- super().__init__(parent)
- def createEditor(self, parent, option, index):
- combobox = QComboBox(parent)
- return combobox
- def setEditorData(self, editor, index):
- data = index.model().data(index, Qt.DisplayRole)
- combobox = editor
- combobox.setCurrentText(data)
- def setModelData(self, editor, model, index):
- combobox = editor
- data = combobox.currentText()
- model.setData(index, data, Qt.EditRole)
- class MainWindow(QMainWindow):
- def __init__(self):
- super().__init__()
- self.setupUi()
- def setupUi(self):
- self.tableView = QTableView(self)
- self.setCentralWidget(self.tableView)
- # 创建数据模型(假设有两列)
- model = QStandardItemModel(4, 2, self)
- model.setHorizontalHeaderLabels(["price", "type"])
- self.tableView.setModel(model)
- # 设置第2列为QComboBox
- delegate = ComboBoxDelegate(self.tableView)
- self.tableView.setItemDelegateForColumn(1, delegate)
- # 示例数据
- model.setData(model.index(0, 1), "Type1")
- model.setData(model.index(1, 1), "Type2")
- model.setData(model.index(2, 1), "Type3")
- model.setData(model.index(3, 1), "Type4")
- # 信号槽连接
- model.dataChanged.connect(self.onDataChanged)
- def onDataChanged(self, index):
- column = index.column()
- if column == 0:
- price = index.data(Qt.DisplayRole)
- type_index = index.sibling(index.row(), 1)
- type_data = type_index.data(Qt.DisplayRole)
- if type_data != "":
- QMessageBox.information(self, "Info", f"Price: {price}, Type: {type_data}")
- if __name__ == "__main__":
- import sys
- app = QApplication(sys.argv)
- mainWindow = MainWindow()
- mainWindow.show()
- sys.exit(app.exec_())
复制代码
在这个示例中,我们创建了一个包含两列的QTableView,并将第2列设置为QComboBox。当用户在第1列输入价格时,第2列对应的QComboBox将显示相应的选项。当数据改变时,我们通过信号槽连接,在数据改变时打印出价格和类型。你可以根据自己的需求进行进一步的处理。注意,这里使用的是PyQt5库,所以在运行之前请确保已经安装了该库。
求最佳答案 |
|