Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableWidgetItem::sortItems. "Losing" data because of misunderstanding of its use.
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 983 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mar4eli
    wrote on last edited by
    #1

    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 ?

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      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.

      Greetz, Jeroen

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mar4eli
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved