Results 1 to 5 of 5

Thread: QTableWidget row swapping and insertion

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTableWidget row swapping and insertion

    Yes, you are using model-view, but the actual model and view is hidden from you, you use convenience classes which keep their own internal model so for example you can't just reorder the internal structures of the model, because you don't have access to them - you have to do that manually by moving each item. If you had a real model, you could do something like this:

    Qt Code:
    1. void myModel::swapRows(QModelIndex i1, QModelIndex i2){
    2. Q_ASSERT(i1.isValid() && i2.isValid());
    3. int r1 = i1.row();
    4. int r2 = i2.row();
    5. if(r1==r2) return;
    6. MyStruct temp = _internaldata.at(r1);
    7. _internaldata[r1] = _internaldata[r2];
    8. _internaldata[r2] = temp;
    9. int cc = columnCount();
    10. emit dataChanged(i1.sibling(r1, 0), i1.sibling(r1, cc-1);
    11. emit dataChanged(i2.sibling(r2, 0), i2.sibling(r2, cc-1);
    12. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Nov 2006
    Location
    Almaty, Kazakhstan
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableWidget row swapping and insertion

    Thanks, but looking like I feel myself not that experienced in Qt to use such a deep way.

    Okay, I have modified the code and now it inserts rows perfectly (you were right, that was my fault). It copies the values from the old row to the new one with setItem() as you told. So I have

    Qt Code:
    1. void DataTableWidget::onRowDown()
    2. {
    3. int crow = currentItem()->row();
    4.  
    5. insertRow(crow + 2);
    6.  
    7. setItem(crow + 2, 0, item(crow, 0));
    8. setItem(crow + 2, 1, item(crow, 1));
    9. setItem(crow + 2, 2, item(crow, 2));
    10.  
    11. removeRow(crow);
    12. }
    To copy to clipboard, switch view to plain text mode 

    I got SIGSEGV when I tried to removeRow(). I have commented it out and this code worked but the new row was somehow linked to the old one. I mean when I was changing data in the new row, the old one was changed in the same way and vice versa. Moreover when I had some changes in the widget and closed the dialogs, I had SIGSEGV too.

    Then I tried to find a way how to remove the old cells and decided to get them by takeItem(), not by item(). So the final code looks like

    Qt Code:
    1. void DataTableWidget::onRowDown()
    2. {
    3. int crow = currentItem()->row();
    4.  
    5. insertRow(crow + 2);
    6.  
    7. setItem(crow + 2, 0, takeItem(crow, 0));
    8. setItem(crow + 2, 1, takeItem(crow, 1));
    9. setItem(crow + 2, 2, takeItem(crow, 2));
    10.  
    11. removeRow(crow);
    12. }
    To copy to clipboard, switch view to plain text mode 
    And this thing works, and removeRow() works too. At least when moving rows down

    Thank you so much, wysota!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.