How to correctly fill a Qtablewidget table in QT with C++

Asked

Viewed 70 times

0

I’m trying to fill a table with data read from a text file. The file structure is formed by lines with pairs of numbers, i.e.:

0.0 1.0
1.0 2.0
2.0 3.0

I am implementing the following code to fill in a Qtablewidget:

QString nomeArquivo = QFileDialog::getOpenFileName(this, "Abrir um arquivo");
QFile file(nomeArquivo);

QTextStream in(&file);
QStringList data = in.readAll().split(QRegExp("[\\n]+"));
data.pop_back();

ui->TabelaDados->setRowCount(data.length());
ui->TabelaDados->setColumnCount(2);

QStringList elem;
QTableWidgetItem *pitem;
for (int i = 0; i < data.length(); i++)
{
   elem = data.at(i).split(QRegExp("\\s+"));

   for (int j = 0; j < ui->TabelaDados->columnCount(); j++)
   {
       pitem = ui->TabelaDados->item(i, j);
       pitem = new QTableWidgetItem();
       ui->TabelaDados->setItem(i, j, pitem);
       pitem->setText(elem.at(j));
   }
}

delete pitem;
file.close();

Up to the first column of the last row, all entries are normally filled in. However, this does not happen in the second column in the last row. The table then looks like this:

0.0 1.0
1.0 2.0
2.0

I’ve tried everything I can to fix it, but so far nothing. The strange thing is that if I print the Debugger to check if the item of the last column in the last row has something, that is:

qInfo() << item->text();

it returns me exactly the missing value in the table.

I’m using the Qtcreator in this project.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.