PDA

View Full Version : Problem setting items in QTableWidget



MartijnKor
12th July 2016, 16:56
Hello everyone,

In the constructor for my Mainwindow I set some parameters for a QTableWidget I placed with the design editor. After that I am calling a memberfunction (testTable) that should fill two rows of the table. Here is the code of the constructor:



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->trackList->setRowCount(0);
ui->trackList->setColumnWidth(0, 260);
ui->trackList->setColumnWidth(1, 260);
ui->trackList->setColumnWidth(2, 68);
rowCount = 0;

playButtonPixmap = new QPixmap(":/images/play_button.png");
playButtonIcon = new QIcon(*playButtonPixmap);
ui->addButton->setIcon(*playButtonIcon);
ui->addButton->setIconSize(QSize(20,20));

QPixmap mainwindowIconPixmap(":/images/play_button.png");
QIcon mainwindowIcon(mainwindowIconPixmap);
setWindowIcon(mainwindowIcon);
setWindowTitle("MK3 - Liveset player");

testTable(ui->trackList);
}


And this is what the function looks like:


void MainWindow::testTable(QTableWidget *table)
{
table->setRowCount(2);

QTableWidgetItem *artist = new QTableWidgetItem;
artist->setText("Akon");

QTableWidgetItem *title = new QTableWidgetItem;
title->setText("Yeah!");

QTableWidgetItem *time = new QTableWidgetItem;
time->setText("00:01:00");

table->setItem(0, 0, artist);
table->setItem(0, 1, title);
table->setItem(0, 2, time);

QTableWidgetItem *artist2 = new QTableWidgetItem;
artist->setText("Justin Bieber");

QTableWidgetItem *title2 = new QTableWidgetItem;
title->setText("Sorry");

QTableWidgetItem *time2 = new QTableWidgetItem;
time->setText("00:03:00");

table->setItem(1, 0, artist2);
table->setItem(1, 1, title2);
table->setItem(1, 2, time2);

}


Now the problem is that whenever I run the application it only fills the top row of the table. It only shows Justin Bieber in the top row and Akon is nowhere to be found. But if I comment out the Justin Bieber part in my code, Akon is visible. What I recon is that it does not respond to the row I set in table->setItem.

Thanks in advance!
Martijn

weiweiqiao
12th July 2016, 18:14
In 19, 22 and 25 line, you should change variable name from *** to ***2. :)

MartijnKor
12th July 2016, 19:00
Currently having the biggest facepalm ever -_- Thanks a lot!!!