PDA

View Full Version : Sorting a qtablewidget......



reshma
22nd March 2009, 10:45
HELLO,

i face a problem while sorting the table...

there are 2 columns....after i enter all the data it sorts it and the result is only the first column is visible..second column remains empty....

why is dis happening???

i m using this...

table = new QTableWidget(1, 2, this);
label << tr("MEMORY") << tr("DATA");
table->setHorizontalHeaderLabels(label);
table->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
table->verticalHeader()->hide();
table->setShowGrid(true);
table->setSortingEnabled(true);
table->sortItems (0,Qt::DescendingOrder );

plz help me/..

Lykurg
22nd March 2009, 10:53
The code you posted seems ok (next time please use the CODE tags). Is the second column shown if you disable sorting? How do you populate the table?

reshma
22nd March 2009, 11:02
is it necessary dat evry tim we add a row sortin needs to b disabled

bcoz i add a row n populate it at run tim after takin data from user????????

Lykurg
22nd March 2009, 11:46
is it necessary dat evry tim we add a row sortin needs to b disabled
No, I just wanted to now if the second colomn is shown when you disable the sorting at all, because I suppose, that the error is in your way of populating the table, not with the sorting option.

reshma
22nd March 2009, 11:48
yes strangely it gets displayed correctly if sortin is disabled....

row=table->rowCount();
table->insertRow(table->rowCount());
QTableWidgetItem *fir = new QTableWidgetItem();
QTableWidgetItem *sec = new QTableWidgetItem();
fir->setText(QString::number(a));
sec->setText(QString::number(b,16));
table->setItem(row, 0, fir);
table->setItem(row, 1, sec);
row++;
table->setSortingEnabled(true);

Lykurg
22nd March 2009, 11:55
Correction:


void QTableWidget::setItem ( int row, int column, QTableWidgetItem * item )

Sets the item for the given row and column to item.

The table takes ownership of the item.

Note that if sorting is enabled (see sortingEnabled) and column is the current sort column, the row will be moved to the sorted position determined by item.

If you want to set several items of a particular row (say, by calling setItem() in a loop), you may want to turn off sorting before doing so, and turn it back on afterwards; this will allow you to use the same row argument for all items in the same row (i.e. setItem() will not move the row).

See also item() and takeItem().


After inserting the first item "row" is not correct anymore for your second item.

reshma
22nd March 2009, 12:15
ok ....i got it....

n i rectified the row problem too...

its workin if i disable sorting before adding a row...

thnks a lot

reshma
23rd March 2009, 12:17
sortin is working correctly only for 3 rows....

when 4-5 or more rows are being added its not sorting correctly...

why is it happening so...??

Lykurg
23rd March 2009, 14:34
What are your values in the sorting column? Be aware using the constructor of QTabelWidgetItem will all input handle as strings. (and orderm them like strings.) If you have integers better use setData with the display role.