Search code examples
pythonqtpyqtpyqt4

PyQt, Qtable clears column when sorting enabled


I have a bug in next function. This function refreshes table with, adjust column sizes and sorts table. But in some cases for some rows text in column 3 and 4 is empty. I found out, that it happens in "problem code" section. What do i wrong in this section? I just want to change size for specific columns. Please help to find out the problem or show me my mistakes.

def print_day_worklog(self):
    logging.info('RefReshing day worklog table')
    selected_day = self.ui.dateDayWorklogEdit.date().toPyDate()
    day_work = db.get_day_worklog(self.creds[3], selected_day)
    self.ui.tableDayWorklog.setRowCount(len(day_work))
    for row, entry in enumerate(day_work):
        e0 = QtGui.QTableWidgetItem(entry[0])
        e1 = QtGui.QTableWidgetItem(entry[1])
        e2 = QtGui.QTableWidgetItem(entry[2].strftime('%H:%M'))
        e3 = QtGui.QTableWidgetItem(entry[3].strftime('%H:%M'))
        e4 = QtGui.QTableWidgetItem(utils.get_time_spent_string(entry[2],
            entry[3]))
        # debug
        print e0.text(), e1.text(), e2.text(), e3.text(), e4.text()
        self.ui.tableDayWorklog.setItem(row, 0, e0)
        self.ui.tableDayWorklog.setItem(row, 1, e1)
        self.ui.tableDayWorklog.setItem(row, 2, e2)
        self.ui.tableDayWorklog.setItem(row, 3, e3)
        self.ui.tableDayWorklog.setItem(row, 4, e4)

    # problem code starts
    self.ui.tableDayWorklog.horizontalHeader().setResizeMode(1,
        QtGui.QHeaderView.Stretch)
    for column in (0,2,3,4):
        self.ui.tableDayWorklog.resizeColumnToContents(column)
        self.ui.tableDayWorklog.horizontalHeader().setResizeMode(column,
            QtGui.QHeaderView.Fixed)
        self.ui.tableDayWorklog.horizontalHeader().setResizeMode(column,
            QtGui.QHeaderView.Fixed)
    self.ui.tableDayWorklog.sortByColumn(2,0)
    # problem code ends

P.s. it looks not clearly, but it's because i try to catch the bug


Solution

  • Problem is in table sorting. I've found this idea in this question. I've just added

    self.ui.tableDayWorklog.setSortingEnabled(False)
    

    before filling the table and this

    self.ui.tableDayWorklog.setSortingEnabled(True)
    

    after that. and it helped