对齐 QTableWidgetItem 会导致项目 None
Aligning a QTableWidgetItem causes item to be None
我在 PyQt5 中创建了一个 table 并像在 中那样填充它。我想居中对齐所有单元格,但是当我尝试对齐每个单元格时,我的 QTableWidgetItem 变为 None。
根据docs setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignHCenter)
should work. See here太
这是一个工作示例,其中 self.db_table 是 QTableWidget。
for row in rows:
inx = rows.index(row)
self.db_table.insertRow(inx)
self.db_table.setItem(inx, 0, QTableWidgetItem(str(row[0])))
self.db_table.setItem(inx, 1, QTableWidgetItem(str(row[1])))
self.db_table.setItem(inx, 2, QTableWidgetItem(str(row[2])))
self.db_table.setItem(inx, 3, QTableWidgetItem(str(row[3])))
self.db_table.setItem(inx, 4, QTableWidgetItem(str(row[4])))
当我添加 setTextAlignment()
时,它使所有 QTableWidgetItems None:
for row in rows:
inx = rows.index(row)
self.db_table.insertRow(inx)
self.db_table.setItem(inx, 0, QTableWidgetItem(str(row[0])).setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignHCenter))
self.db_table.setItem(inx, 1, QTableWidgetItem(str(row[1])).setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignHCenter))
self.db_table.setItem(inx, 2, QTableWidgetItem(str(row[2])).setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignHCenter))
self.db_table.setItem(inx, 3, QTableWidgetItem(str(row[3])).setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignHCenter))
self.db_table.setItem(inx, 4, QTableWidgetItem(str(row[4])).setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignHCenter))
为什么这不起作用?
setTextAlignment()
方法没有 return 任何参数,它等于 None,然后你将 None 传递给 setItem
,你必须要做的是将项目创建为 item = QTableWidgetItem({})
,然后使用 item.setTextAlignment({})
,最后为每个元素添加 self.db_table.setItem(inx, 0, item)
,简化形式如下:
for row in rows:
inx = rows.index(row)
self.db_table.insertRow(inx)
for i, v in zip(range(5), row):
item = QTableWidgetItem(str(v))
item.setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignHCenter)
self.db_table.setItem(inx, i, item)
我在 PyQt5 中创建了一个 table 并像在
根据docs setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignHCenter)
should work. See here太
这是一个工作示例,其中 self.db_table 是 QTableWidget。
for row in rows:
inx = rows.index(row)
self.db_table.insertRow(inx)
self.db_table.setItem(inx, 0, QTableWidgetItem(str(row[0])))
self.db_table.setItem(inx, 1, QTableWidgetItem(str(row[1])))
self.db_table.setItem(inx, 2, QTableWidgetItem(str(row[2])))
self.db_table.setItem(inx, 3, QTableWidgetItem(str(row[3])))
self.db_table.setItem(inx, 4, QTableWidgetItem(str(row[4])))
当我添加 setTextAlignment()
时,它使所有 QTableWidgetItems None:
for row in rows:
inx = rows.index(row)
self.db_table.insertRow(inx)
self.db_table.setItem(inx, 0, QTableWidgetItem(str(row[0])).setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignHCenter))
self.db_table.setItem(inx, 1, QTableWidgetItem(str(row[1])).setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignHCenter))
self.db_table.setItem(inx, 2, QTableWidgetItem(str(row[2])).setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignHCenter))
self.db_table.setItem(inx, 3, QTableWidgetItem(str(row[3])).setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignHCenter))
self.db_table.setItem(inx, 4, QTableWidgetItem(str(row[4])).setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignHCenter))
为什么这不起作用?
setTextAlignment()
方法没有 return 任何参数,它等于 None,然后你将 None 传递给 setItem
,你必须要做的是将项目创建为 item = QTableWidgetItem({})
,然后使用 item.setTextAlignment({})
,最后为每个元素添加 self.db_table.setItem(inx, 0, item)
,简化形式如下:
for row in rows:
inx = rows.index(row)
self.db_table.insertRow(inx)
for i, v in zip(range(5), row):
item = QTableWidgetItem(str(v))
item.setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignHCenter)
self.db_table.setItem(inx, i, item)