QTableWidgetItem::sortItems. "Losing" data because of misunderstanding of its use.



  • Hello.
    First of all, please forgive me my english.

    In my project i use QTableWidget, which should be sorted by first column by default.

    I add QTableWidgetItems in this manner:
    @int RowsCount = 0;
    ui->relTabRelTable->setRowCount(ui->relTabRelTable->rowCount()+1); //add row to table
    RowsCount = ui->relTabRelTable->rowCount()-1; //as usual, length != index.

    //UID
    QTableWidgetItem *tmpTableWidgetItemUID = new QTableWidgetItem;
    tmpTableWidgetItemUID->setText(tmpRel.relUID); //add text from my variable.
    ui->relTabRelTable->setItem(RowsCount,0,tmpTableWidgetItemUID); //add information to the last row.
    
    //Formula
    QTableWidgetItem *tmpTableWidgetItemFormula = new QTableWidgetItem;
    tmpTableWidgetItemFormula->setText(tmpRel.formula); //add text from my variable
    ui->relTabRelTable->setItem(RowsCount,1,tmpTableWidgetItemFormula);
    

    @

    As you can see, i decided to add everything on the last row.

    At first, everything had been working fine, until i decided to use this:
    @ui->relTabRelTable->sortItems(0);@

    According to it's description:
    @void QTableWidget::sortItems(int column, Qt::SortOrder order = Qt::AscendingOrder)
    Sorts all the rows in the table widget based on column and order.@

    I* decided, that it sorts when i call it*. But reality is different) After the use of this public slot, qtablewidget sorts whenever i add something new. Because of this, when i add information as i mentioned above, i get first column on the sorted row and all other information, on the last row.

    i have found two solutions:

    1. first add sorted column, than get it's row and add all other information on it. But this is useful only if you never change sorting column.
    2. before adding new information i add setSortingEnabled(false) and setSortingEnabled(true) after all.

    Questions:
    Is there any other simple way to do my task?
    Did i correctly understand how sortItems works?
    Can i disable only sortItems and still have sortingEnabled == true ?



  • Hi,
    Try to set the column to 1, not 0 for the sortItems function. The first data row is 1
    That might do the trick.
    The setSortingEnabled() should only need to be set once after construction.



  • Thank you for your prompt reply.

    [quote author="Jeroentje@home" date="1396274605"]Hi,
    Try to set the column to 1, not 0 for the sortItems function. The first data row is 1
    That might do the trick. [/quote]
    I checked this. It can be strange, but 0 - is the first column. And 0 is the first row with data.

    [quote author="Jeroentje@home" date="1396274605"]
    The setSortingEnabled() should only need to be set once after construction.
    [/quote]

    I also think so, but it is the easiest solution.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.