My program should gradually output the results of calculations in the table cells. There is a window (implemented as an object of the Widget class inherited from QWidget), this object has a property r_table (pointer to QTableWidget) - this is the table itself.

How many lines you need is not known in advance, so I want them added as needed.

I need a method

void Widget::setTableValue(int i, int j, double value) 

which puts the number value in the cell with row number i and column number j , and if there is no row i , it creates it.

How to implement this?

I tried to do it like this

 void Widget::setTableValue(int i, int j, double value) { while (r_table->rowCount() < i) r_table->insertRow(r_table->rowCount()); r_table->setItem(i, j, new QTableWidgetItem(QString::number(value, 'g', 2))); } 

but it fills the table like it is.

  • if (r_table-> rowCount () <= i) r_table-> setColCount (i + 1); - Evgeny Shmidt
  • one
    maybe setRowCount? - havon
  • off topic, but still ... QTableWidget is a toy ... for any task more difficult than the most primitive display of several lines in a table, IMHO it’s better to use a normal model with QTableView ... but it will take some time to study the framework and refuse bad habits ... - Fat-Zer
  • one
    @ Fat-Zer it will be from the gun on the sparrows - havon

1 answer 1

Like this

 void Widget::setTableValue(int i, int j, double value) { if (r_table->rowCount() <= i) r_table->setRowCount(i + 1); r_table->setItem(i, j, new QTableWidgetItem(localeRus->toString(value, 'g', 4))); } 

localeRus is a QLocale object with a Russian locale so that the decimal separator is a comma.

  • good practice would be to use QLocale::system() instead of nailing to the Russian locale ... - Fat-Zer