Syed Iftekharuddin
Syed Iftekharuddin

Reputation: 146

Displaying items in QTableWidget

I need to add items to the QTableWidget,The Items are QList qList, I tried some code as

int row = 0;
QList<Players>::iterator j;
                for (j = qList.begin(); j != qList.end(); ++j){
                    ui->tableWidget->insertRow(qList.count);
                    ui->tableWidget->setItem(row, 0, new QTableWidgetItem(j->playerName) );
                    ui->tableWidget->setItem(row, 1, new QTableWidgetItem(j->playerRank) );
                    row++;

        }

where playerName and playerRank are some properties in Players class.

class Players
{
public:
    Players();
    int playerId;
    QString playerName;
    int playerRank;
};

But nothing displays in the QTableWidget. How To do this?

Upvotes: 0

Views: 245

Answers (1)

Jolly Roger
Jolly Roger

Reputation: 1134

I cannot comment if there is some issue with something else without seeing the entire code but this line seems wrong

ui->tableWidget->insertRow(qList.count);

From qtablewidget documentation

void QTableWidget::insertRow(int row)
Inserts an empty row into the table at row.

qlist.count appears to be the total length of list. You are inserting a row at position qlist.count and not adding qlist.count rows. Even this logic seems wrong as you are adding n^2 rows for n elements (adding inside for loop). You should look at setRowCount

Make this line like this

ui->tableWidget->insertRow(row);

Upvotes: 1

Related Questions