我需要一些帮助,因为这个问题根本没有道理...缩进看起来有点抱歉...
self.myData = [["facebook-icon", "Facebook", str(self.mykeys[0][1]), "*" *len(self.passes[0])], ......
即使rowCount打印出数字3,也仅填充表中的第一项。那就是让我感到困惑的..希望有人尝试这段代码,我是Python新手
passAmount = len(self.myData)
print("There are %x items in myData" % (passAmount))
rowCount = self.tableWidget.rowCount()
print("There are %x items in the table" % (rowCount))
for row in range(0, rowCount):
cellText = self.tableWidget.itemAt(row,0).text()
if(cellText == "facebook-icon"):
self.tableWidget.itemAt(row, 0).setText("")
print(imagePaths[0])
fb = QPixmap(imagePaths[0]).scaledToWidth(20)
label = QLabel()
label.setPixmap(fb)
# label.setScaledContents(True)
self.tableWidget.setCellWidget(row, 0, label)
elif(cellText == "blogger-icon"):
...
self.tableWidget.setFont(self.font)
self.tableWidget.resizeColumnsToContents()
self.tableWidget.resizeRowsToContents()
self.tableWidget.doubleClicked.connect(self.on_table_click)
# Show widget
self.show()
我做错什么了吗?
答案 0 :(得分:2)
如我所见,您想从第一列中获取元素,但是itemAt()
不会将给定行和列的项返回到几何位置,而是应使用item()
方法。另外,我们可以使用字典来减少代码:
dict_icons = {
"facebook-icon": imagePaths[0],
"blogger-icon": imagePaths[1]
}
for row in range(0, 3):
item = self.tableWidget.item(row, 0)
image_path = dict_icons.get(item.text())
if image_path is not None:
item.setText("")
pixmap = QPixmap(image_path).scaledToWidth(20)
label = QLabel(pixmap=pixmap)
self.tableWidget.setCellWidget(row, 0, label)