PDA

View Full Version : QTableWidget not clearing table data: How can i solve this problem?



robgeek
18th April 2015, 00:23
Hi!

I'm using a spinbox(in data.cpp) to show some data in a table, but everytime i change the spinbox value, the table shows me the new values with the old ones. My table doens't erase the old data. I don't want that, i want just the new data! How can i do that?


// data.h
class Data : public QDialog
{
Q_OBJECT

public:
Table confTable;
QList<Number> ranking;
//...



// data.cpp
void Data::on_spinRkgLM_valueChanged(int arg1)
{

ranking = hs->rankingNumbers(hs, arg1);
confTable.showRkgTable(ui->tableWidget, ranking );

}


// "Table" is a class i use only to put all configurations(omitted here) and showing codes.
void Table::showRkgTable(QTableWidget *table, QList<Number> numbers)
{
int size = numbers.size( );

for(int i = 0; i < size; i++)
{
table->insertRow( i );
QTableWidgetItem *val = new QTableWidgetItem( QString::number( numbers[ i ].val ) );
QTableWidgetItem *acum = new QTableWidgetItem( QString::number( numbers[ i ].acum) );
table->setItem(i, 0, val);
table->setItem(i, 1, acum);
}
}

jefftee
18th April 2015, 04:05
I don't see where you make any attempt to remove old entries. Have you looked at QTableWidget::clearContents or QTableWidget::removeRow?

robgeek
18th April 2015, 04:17
Yes, sorry not mention that.

I tried:

// "Table" is a class i use only to put all configurations and showing codes.
void Table::showRkgTable(QTableWidget *table, QList<Number> numbers)
{
int size = numbers.size( );

//for(int i = 0; i < size; i++) Didn't work.
// table->removeRow( i );

//table->clearContents( ); Didn't work.

for(int i = 0; i < size; i++)
{
table->insertRow( i );
QTableWidgetItem *val = new QTableWidgetItem( QString::number( numbers[ i ].val ) );
QTableWidgetItem *acum = new QTableWidgetItem( QString::number( numbers[ i ].acum) );
table->setItem(i, 0, val);
table->setItem(i, 1, acum);
}
}

jefftee
18th April 2015, 04:20
I have not tested this, but in your removeRow() attempt, I suspect that after you remove row 0, what started as row 1 is now the new row 0?

Have you tried something like:



for(int i = 0; i < size; i++)
table->removeRow( 0 );


Edit: perhaps also try the following:



table->setRowCount(0);

robgeek
18th April 2015, 04:33
Thanks, dude!

I tried: removeRow( 0 ) and now is working fine! But why clearContents did not work?

jefftee
18th April 2015, 04:39
I have not used clearContents() personally (nor QTableWidget for that matter), but the doc says this for clearContents:



The table dimensions stay the same.


I interpret that to mean that it does not actually remove rows or columns, but I would have thought it would remove the actual contents of the rows/columns.

robgeek
18th April 2015, 04:42
Yes, i read that too! Still don't understand, but thank you anyway.

anda_skoa
18th April 2015, 11:39
You don't want to do what you are currently doing.

As long as numbers.size() is equal to the table's row count, there is no need at all to remove or add any rows.
If there is a difference you either remove or add rows until these two numbers are equal again.

And then you update the rows.

Cheers,
_