Skip to content Skip to sidebar Skip to footer

Update Qtablewidget Rows With Each Iteration Of Loop

I want to simultaneously update the QTableWidget with each iteration of loop as a row is inserted in the table I need that because if I wait for for changes to show up after the lo

Solution 1:

Important information:

With a QTableWidget the solution doesn't work. So here is shown how the code must be changed for speed-up and for using a QTableWidget.

Explanation

Because you can't add a couple of rows at once with a QTableWidget it's needed to take the QTableView. The important difference here is that you can choose the item model to suit your needs. Here a QStandardItemModel is chosen because it supports the missing function appendRow(...). This is what we want because the paint event is called after a couple of items are inserted and not as before after an item has been inserted or edited.

Needed code before

self.tableResults = QTableView(self);
self.tableResults.setModel(QStandardModel(0, 7, self));

Additional information:

The QTableView class needs a class which inherits from QAbstractItemModel because this class manages the data. So to use QTableView it's needed to set an item model.

Why QStandardModel? The QStandardModel class provides more functions which help add multiple items at once.

Changed Code

defbtnStartClick(self):

    startYear = self.startDatePicker.date().year();
    startMonth = self.startDatePicker.date().month();
    startDay = self.startDatePicker.date().day();
    startDate = date(startYear,startMonth,startDay)

    endYear = self.endDatePicker.date().year();
    endMonth = self.endDatePicker.date().month();
    endDay = self.endDatePicker.date().day();
    endDate = date(endYear,endMonth,endDay)

    delta = endDate - startDate
    gameResults = []
    for i inrange(delta.days + 1):
        tdate = startDate + td(days=i)
        resultDate = tdate.strftime("%m/%d/%y")
        results = getResults(resultDate)
        for gameId in results:
            List row;

            gameResult = getGameResult(gameId,date)
            row.append(QtWidgets.QStandardItem(str(gameResult.gameNumber)))
            row.append(QtWidgets.QStandardItem(str(gameResult.gameName)))
            row.append(QtWidgets.QStandardItem(str(gameResult.date)))
            row.append(QtWidgets.QStandardItem(str(gameResult.first)))
            row.append(QtWidgets.QStandardItem(str(gameResult.second)))
            row.append(QtWidgets.QStandardItem(str(gameResult.third)))
            row.append(QtWidgets.QStandardItem(str(gameResult.fourth)))
            (self.tableResults.model()).appendRow(row); #maybe a cast to this class is needed QStandardItemModel. It's python so I'm not sure
            gameResults.append(gameResult)

    print(gameResults)

Post a Comment for "Update Qtablewidget Rows With Each Iteration Of Loop"