Results 1 to 2 of 2

Thread: QTableView: saving/restoring columns' widths

  1. #1
    Join Date
    Apr 2009
    Location
    Italy
    Posts
    70
    Thanks
    23
    Thanked 15 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QTableView: saving/restoring columns' widths

    Hello,
    how can I save and restore the columns' widths of a QTableView? Basically, I want to store them in my QSettings and restore them on program start.

    I already have the working code for saving and restoring the geometry of a QDialog, but it does not work for a QTableView.

    I tried both:

    Qt Code:
    1. myTableView->saveGeometry()
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. myTableView->horizontalHeader()->saveGeometry()
    To copy to clipboard, switch view to plain text mode 

    but then, when I call restoreGeometry() in the QTableView constructor, nothing happens.

    Thank you

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTableView: saving/restoring columns' widths

    Take a look at QHeaderView::saveState() and QHeaderView::restoreState(). This also stores column order and, I think, sort order.

    I found that I could not restore the state in the constructor, presumably because the QHeaderView was not fully ready. In my case, I got around it by using a zero delay single-shot timer at the end of the constructor to trigger the restore when the event loop became idle. There is probably a more elegant way to do this:
    Qt Code:
    1. // Schedule the restoration of the view state
    2. // Done this way because if called directly by the constructor the
    3. // QHeaderView is not fully constructed
    4. QTimer::singleShot(0, this, SLOT(readSettings()));
    To copy to clipboard, switch view to plain text mode 
    If the column count can change between runs then be aware that restoring a mismatched state might cause problems. You may also need to force focus to the table view to make the columns reorder to match the header if column order is not fixed.

  3. The following 2 users say thank you to ChrisW67 for this useful post:

    johnmauer (20th January 2010), mattc (18th December 2009)

Similar Threads

  1. How to change columns order in a QTableView...
    By cydside in forum Qt Programming
    Replies: 1
    Last Post: 20th April 2009, 10:42
  2. updating QTableView with new qsl columns
    By sylvainb in forum Qt Programming
    Replies: 2
    Last Post: 28th January 2009, 20:51
  3. How to display selected columns in QTableView widget.
    By kaushal_gaurav in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 08:30
  4. Different delegates to different Columns of QTableView.
    By kaushal_gaurav in forum Qt Programming
    Replies: 4
    Last Post: 6th August 2008, 09:17
  5. Ensure columns in qtableview uses all available space
    By oysteinpettersen in forum Qt Programming
    Replies: 2
    Last Post: 10th January 2008, 11:28

Tags for this Thread

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.