Results 1 to 5 of 5

Thread: Renumbering Row Headers of QHeaderView

  1. #1
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Renumbering Row Headers of QHeaderView

    Greetings,

    I am using a QTableWidget with both vertical and horizontal headers. How can I manually renumber the vertical row headers after a move?

    When QHeaderView does a sort (when I click on a column header) the row header numbers do not change, only the contents of the cells. I am trying to do a similar thing using QHeaderView::moveSection() (kind of a custom sort).

    Qt Code:
    1. QHeaderView* pHeaderView = verticalHeader();
    2. pHeaderView->moveSection( nSrcRow, nDestRow );
    To copy to clipboard, switch view to plain text mode 
    While the row contents are exchanged as I expect, the row header numbers also change. I would like to always keep a 1,2,3 kind of labeling like the built-in QHeaderView sort.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Renumbering Row Headers of QHeaderView

    If you use QTableView instead of QTableWidget, you can simpy return header names in a different order (see QAbstractItemModel::headerData for details).

  3. #3
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Renumbering Row Headers of QHeaderView

    Thanks for the quick reply.

    Using QTableView is currently not an option because of time restraints. After the release of the product, we will try to convert all our QTableWidget code to QTableView for subsequent releases.

    We may have to leave this feature out of this release.

  4. #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: Renumbering Row Headers of QHeaderView

    You could connect to signal QHeaderView::sectionMoved() and rename headers every time a section movement occurs. Just make sure you create and set header items.

    Qt Code:
    1. // it is important to create and set header items
    2. // otherwise horizontalHeaderItem(int) and verticalHeaderItem(int) will always return 0
    3. for (int r = 0; r < rowCount(); ++r)
    4. {
    5. QTableWidgetItem* item = new QTableWidgetItem(QString::number(r));
    6. setVerticalHeaderItem(r, item);
    7. }
    8.  
    9. for (int c = 0; c < columnCount(); ++c)
    10. {
    11. QTableWidgetItem* item = new QTableWidgetItem(QString::number(c));
    12. setHorizontalHeaderItem(c, item);
    13. }
    14.  
    15. connect(horizontalHeader(), SIGNAL(sectionMoved(int, int, int)), SLOT(renameHeaders()));
    16. connect(verticalHeader(), SIGNAL(sectionMoved(int, int, int)), SLOT(renameHeaders()));
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // the slot doing the renaming
    2. void TableWidget::renameHeaders()
    3. {
    4. // "convert" logical indexes to visual indexes
    5. for (int r = 0; r < rowCount(); ++r)
    6. verticalHeaderItem(r)->setText(QString::number(verticalHeader()->visualIndex(r)));
    7.  
    8. for (int c = 0; c < columnCount(); ++c)
    9. horizontalHeaderItem(c)->setText(QString::number(horizontalHeader()->visualIndex(c)));
    10. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #5
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Renumbering Row Headers of QHeaderView

    Thanks, jpn, this looks like a workable solution for my circumstances.

Similar Threads

  1. QTableView : headers disappear
    By xavier in forum Qt Programming
    Replies: 2
    Last Post: 8th May 2006, 16:57
  2. QMake / headers / lot of files
    By jcr in forum Qt Programming
    Replies: 7
    Last Post: 10th January 2006, 12:06

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.