Results 1 to 14 of 14

Thread: QTableWidget move row?

  1. #1
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question QTableWidget move row?

    Hi,

    i have a QTablewidget with "SingleSelection". How can i move the rows up and down?

    Thanks in advance,

    Whitefurrows

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableWidget move row?

    There isn't any single API method for doing that. Use QTableWidget::takeItem() to temporarily remove both "source" and "destination" items from the table without deleting them, and then use QTableWidget::setItem() to set them back in a reverse order.
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    whitefurrows (23rd August 2006)

  4. #3
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: QTableWidget move row?

    Hi,

    the problem is i have many items in a row. I try this to remove the row.

    Qt Code:
    1. QModelIndexList idxList = tableWidget->selectionModel()->selectedIndexes();
    2.  
    3. QListIterator<QModelIndex> idxIt(indexList);
    4. idxIt.toBack();
    5.  
    6. while(idxIt.hasPrevious()){
    7. QModelIndex idx = idxIt.previous();
    8. if(index.column() == 0){
    9. tableWidget->removeRow(idx.row());
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    but im not sure how can i paste it to move up and down. Can you help me please?

    Thanks in advance,

    Whitefurrows

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableWidget move row?

    Qt Code:
    1. void TableWidget::move(bool up)
    2. {
    3. Q_ASSERT(selectedItems().count() > 0);
    4. const int sourceRow = row(selectedItems().at(0));
    5. const int destRow = (up ? sourceRow-1 : sourceRow+1);
    6. Q_ASSERT(destRow >= 0 && destRow < rowCount());
    7.  
    8. // take whole rows
    9. QList<QTableWidgetItem*> sourceItems = takeRow(sourceRow);
    10. QList<QTableWidgetItem*> destItems = takeRow(destRow);
    11.  
    12. // set back in reverse order
    13. setRow(sourceRow, destItems);
    14. setRow(destRow, sourceItems);
    15. }
    16.  
    17. // takes and returns the whole row
    18. QList<QTableWidgetItem*> TableWidget::takeRow(int row)
    19. {
    20. QList<QTableWidgetItem*> rowItems;
    21. for (int col = 0; col < columnCount(); ++col)
    22. {
    23. rowItems << takeItem(row, col);
    24. }
    25. return rowItems;
    26. }
    27.  
    28. // sets the whole row
    29. void TableWidget::setRow(int row, const QList<QTableWidgetItem*>& rowItems)
    30. {
    31. for (int col = 0; col < columnCount(); ++col)
    32. {
    33. setItem(row, col, rowItems.at(col));
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  6. The following 2 users say thank you to jpn for this useful post:

    vycke (28th April 2008), whitefurrows (23rd August 2006)

  7. #5
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Re: QTableWidget move row?

    Hi,

    thank you for that great example, that's work fine.

    Greetings,

    Whitefurrows

  8. #6
    Join Date
    Aug 2006
    Posts
    15
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Re: QTableWidget move row?

    I have the same problem, but what i need to swap are the cell widgets, and getting the items don't work, the items get changed, but the cell widgets keep the same.

    Is there any way of doing this?

    Qt Code:
    1. QTableWidgetItem *item1 = colorizerTable->takeItem(row1, 0);
    2. QTableWidgetItem *item2 = colorizerTable->takeItem(row2, 0);
    3. colorizerTable->setItem(row1, 0, item2);
    4. colorizerTable->setItem(row2, 0, item1);
    To copy to clipboard, switch view to plain text mode 

  9. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableWidget move row?

    Apparently QTableWidget::setCellWidget() maps the widget to the specific cell, not to the specific item. So you will have swap the cell widget by hand, too.
    J-P Nurmi

  10. The following user says thank you to jpn for this useful post:

    deasterbrook (5th January 2007)

  11. #8
    Join Date
    Aug 2006
    Posts
    15
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableWidget move row?

    The problem is that, when i set a widget for a specific cell, it deletes the previous one, so I cannot

    The only way I've found is to have a Clone function in the widget's class

  12. #9
    Join Date
    Jun 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default it is only can moveup or movedown.cant move two or more rows.

    Quote Originally Posted by jpn View Post
    Qt Code:
    1. void TableWidget::move(bool up)
    2. {
    3. Q_ASSERT(selectedItems().count() > 0);
    4. const int sourceRow = row(selectedItems().at(0));
    5. const int destRow = (up ? sourceRow-1 : sourceRow+1);
    6. Q_ASSERT(destRow >= 0 && destRow < rowCount());
    7.  
    8. // take whole rows
    9. QList<QTableWidgetItem*> sourceItems = takeRow(sourceRow);
    10. QList<QTableWidgetItem*> destItems = takeRow(destRow);
    11.  
    12. // set back in reverse order
    13. setRow(sourceRow, destItems);
    14. setRow(destRow, sourceItems);
    15. }
    16.  
    17. // takes and returns the whole row
    18. QList<QTableWidgetItem*> TableWidget::takeRow(int row)
    19. {
    20. QList<QTableWidgetItem*> rowItems;
    21. for (int col = 0; col < columnCount(); ++col)
    22. {
    23. rowItems << takeItem(row, col);
    24. }
    25. return rowItems;
    26. }
    27.  
    28. // sets the whole row
    29. void TableWidget::setRow(int row, const QList<QTableWidgetItem*>& rowItems)
    30. {
    31. for (int col = 0; col < columnCount(); ++col)
    32. {
    33. setItem(row, col, rowItems.at(col));
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 
    it is only can moveup or movedown.cant move tow or more rows.actually,it is called swap two rows seemed more reasonable.

  13. #10
    Join Date
    May 2007
    Posts
    131
    Thanks
    17
    Thanked 4 Times in 2 Posts

    Default Re: QTableWidget move row?

    You can move row using method "moveSection" in table->verticalHeader()

  14. #11
    Join Date
    May 2012
    Posts
    13
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableWidget move row?

    Hi,
    "moveSection" will move the section of the header and its contents (<- the row including QWidgets set with "setCellWidget"). But what if i want to move only the contents of the row without changing the header? More specifically, what should i do if want to swap two cells containing each of them a QWidget?

    jpn

    Re: QTableWidget move row?
    Apparently QTableWidget::setCellWidget() maps the widget to the specific cell, not to the specific item. So you will have swap the cell widget by hand, too.
    How can i swap the cell widget by hand?
    Thanx in advance

  15. #12
    Join Date
    May 2012
    Posts
    13
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableWidget move row?

    Till now, i've manage to swap two cells with QWidgets but with a very ugly trick!
    First, i get the pointers of the two QWidgets by calling "cellWidget" (let's say wid1 and wid2). Then, i call "editorDestroyed(wid1)" and "editorDestroyed(wid2)". This protected slot releases QWidget's pointer from the internal map but does NOT delete the widget! Finally, i call setCellWidget to set the QWidgets where i want to.

    I know that this method is not cute, but it's the only i've got till now

  16. #13
    Join Date
    Apr 2017
    Posts
    2
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: QTableWidget move row?

    Damn !
    the function 'editorDestroyed' is protected and not accessible

  17. #14
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTableWidget move row?

    the function 'editorDestroyed' is protected and not accessible
    Then all you have to do is derive your own table widget class from QTableWidget and you have access to all of the protected methods of QTableWidget and its base classes. This is basic C++.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QTableWidget Sorting Multiple Selection
    By rhiacasta in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2006, 22:05
  2. QTableWidget Update - slow
    By DPinLV in forum Qt Programming
    Replies: 16
    Last Post: 18th August 2006, 22:09
  3. Move child widget along with the parent widget
    By sreedhar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2006, 13:00
  4. Replies: 6
    Last Post: 5th March 2006, 22:05
  5. How to obtain the width of a QTableWidget?
    By Giel Peters in forum Qt Programming
    Replies: 3
    Last Post: 9th January 2006, 23:34

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.