我真的想尽一切办法来解决我的问题,但它不起作用。下面是我在表的每一行中放置组合框的简单代码。它实际上适用于setItem(),我使用它将字符串放入每一行。但它不适用于setCellWidget(),我必须使用它将组合框放入行中。这就好像setCellWdiget()在将组合框放入行中之后删除了它,因为它最终只出现在最后一行,我不明白为什么。如果你们中有人能帮我那就太好了。首先要感谢大家!
代码如下:
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()
发布于 2016-09-27 08:50:14
您创建了一个单独的组合框,因此当您将其放入一个单元格时,它将从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
https://stackoverflow.com/questions/39720036
复制相似问题