简体   繁体   English

PyQt5 QTableWidget仅显示第一行和第一列

[英]PyQt5 QTableWidget showing only first line and column

I have a QTableWidget populated with a list of lists. 我有一个QTableWidget填充了列表列表。 Each inner list have eight elements and my table have a ninth control column calculated after the others are loaded. 每个内部列表具有八个元素,而我的表具有第九个控制列,该列在其他元素加载后计算。 I can read and print to console the content of any cell of the table like print(self.tAccounts.item(52,3).text()) , so I think there is no problem with the data, but the table shows only the cell's content for the first line and column in the table, leaving the others bank. 我可以读取并打印以控制台表的任何单元格的内容,例如print(self.tAccounts.item(52,3).text()) ,所以我认为数据没有问题,但该表仅显示表格中第一行和第一列的单元格内容,其余的保留。 I should be making a mistake in some place, but I can't see. 我应该在某个地方犯了一个错误,但是我看不到。

Using PyQt 5 and Python 3. 使用PyQt 5和Python 3。

The constructor 构造函数

class Table(QWidget):
    def __init__(self, parent=None):
        super(Table, self).__init__(parent)

        self.accounts = [] # The source is created in the constructor\
                           # and populate in other member function
        self.tAccounts = QTableWidget(0,9)
        self.tAccounts.setSortingEnabled(True)
        self.tAccounts.setHorizontalHeaderLabels(['1','2','3','4','5','6','7','8','9'])
        self.tAccounts.resizeColumnsToContents()
        self.tAccounts.verticalHeader().hide()

The member function: 成员函数:

def loadDay(self):
    for row, account in enumerate(self.accounts):
        self.tAccounts.insertRow(row)
        for col in range(8):
            self.tAccounts.setItem(row, col, QTableWidgetItem(str(accounts[col])))
            self.tAccounts.item(row,col).setTextAlignment(Qt.AlignRight)
        self.tAccounts.setItem(row, 8, QTableWidgetItem('')) # defined for further use

Finally I found it. 终于我找到了。

The problem is in enabling the sorting in the constructor. 问题在于在构造函数中启用排序。 Seems the default sorting is ZA. 似乎默认排序为ZA。 Changing the sort to AZ by clicking in the header of the empty table solves the bug but the best solution is to move the line self.tAccounts.setSortingEnabled(True) to the end of the loadDay function. 通过单击空表的标题将排序方式更改为AZ可解决该错误,但是最好的解决方案是将self.tAccounts.setSortingEnabled(True)self.tAccounts.setSortingEnabled(True) loadDay函数的末尾。 Seems to be a clash between the ever changing row number because of enabled sorting and the updating algorithm of QTableWidget 由于启用了排序,似乎行数不断变化与QTableWidget的更新算法之间存在冲突

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM