Python QTableWidget中的PyQt5 QComboBox
我真的想尽一切办法来解决我的问题,但都没用。 下面是我在表的每一行中放置组合框的简单代码。它实际上适用于setItem(),我使用它将字符串放入每一行。但它不适用于setCellWidget(),我必须使用它将组合框放入行中。这就好像setCellWdiget()在将组合框放入行后删除了它,因为它最终只出现在最后一行,我不明白为什么。 如果你们中有人能帮我,那就太好了。非常感谢 代码如下:Python QTableWidget中的PyQt5 QComboBox,python,qt,pyqt5,qtablewidget,qcombobox,Python,Qt,Pyqt5,Qtablewidget,Qcombobox,我真的想尽一切办法来解决我的问题,但都没用。 下面是我在表的每一行中放置组合框的简单代码。它实际上适用于setItem(),我使用它将字符串放入每一行。但它不适用于setCellWidget(),我必须使用它将组合框放入行中。这就好像setCellWdiget()在将组合框放入行后删除了它,因为它最终只出现在最后一行,我不明白为什么。 如果你们中有人能帮我,那就太好了。非常感谢 代码如下: import sys from PyQt5 import QtWidgets, QtCore class
import sys
from PyQt5 import QtWidgets, QtCore
class Window(QtWidgets.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(50,50,500,500)
self.setWindowTitle('PyQt Tuts')
self.table()
def table(self):
comboBox = QtWidgets.QComboBox()
self.tableWidget = QtWidgets.QTableWidget()
self.tableWidget.setGeometry(QtCore.QRect(220, 100, 411, 392))
self.tableWidget.setColumnCount(2)
self.tableWidget.setRowCount(5)
self.tableWidget.show()
attr = ['one', 'two', 'three', 'four', 'five']
i = 0
for j in attr:
self.tableWidget.setItem(i, 0, QtWidgets.QTableWidgetItem(j))
self.tableWidget.setCellWidget(i, 1, comboBox)
i += 1
def run():
app = QtWidgets.QApplication(sys.argv)
w = Window()
sys.exit(app.exec_())
run()
您创建一个组合框,因此当您将其放入单元格时,它将从prevoius单元格中删除。 必须为每个单元格(在
for
循环中)创建一个组合框
例如:
attr = ['one', 'two', 'three', 'four', 'five']
i = 0
for j in attr:
self.tableWidget.setItem(i, 0, QtWidgets.QTableWidgetItem(j))
comboBox = QtWidgets.QComboBox()
self.tableWidget.setCellWidget(i, 1, comboBox)
i += 1
你有什么意思的例子吗?+1根据文档:
将给定的小部件显示在给定行和列的单元格中,并将小部件的所有权传递给表。