[SOLVED] QHeaderView saveState() and restoreState() example



  • Basically my requirement is to save column the settings for QTableView, after researching I found the solution to use save/restore state of QHeaderView. However I couldn't find an example for this. It will be nice if someone can provide more information for the same.

    I would also like to know if we can save the sequence no. of the column , restore visible/hidden column with QHeaderView.

    Best Regards,



  • Hi!

    An example of how to store the values of a QTableView (that are configured by its QHeaderView is the following (int this case in a .ini file):

    @QSettings settings2("dialogs.ini", QSettings::IniFormat);
    settings2.setValue("columnConfig", headerView->saveState());@

    To restore the values you can code:

    @QSettings settings("dialogs.ini", QSettings::IniFormat);
    headerView->restoreState(settings.value("columnConfig").toByteArray());@

    The configuration stored is exactly what you want of the QHeaderView. Is this class that stores the values of column order, size, visible/hidden, etc.


  • Moderators

    [quote author="Sam" date="1370333044"]
    I would also like to know if we can save the sequence no. of the column , restore visible/hidden column with QHeaderView.[/quote]
    yes this is also possible, but an awkward work ... at least in my eyes ;)
    You need to keep track of the "logical":http://qt-project.org/doc/qt-4.8/qheaderview.html#logicalIndex and "visual":http://qt-project.org/doc/qt-4.8/qheaderview.html#visualIndex index and store/restore them appropriately.
    E.g. every time a section has been moved, hidden, resized etc.



  • As I said before, saving the State of QHeaderView solves it!! It stores also de logical and visual indices.

    [quote author="raven-worx" date="1370338797"][quote author="Sam" date="1370333044"]
    I would also like to know if we can save the sequence no. of the column , restore visible/hidden column with QHeaderView.[/quote]
    yes this is also possible, but an awkward work ... at least in my eyes ;)
    You need to keep track of the "logical":http://qt-project.org/doc/qt-4.8/qheaderview.html#logicalIndex and "visual":http://qt-project.org/doc/qt-4.8/qheaderview.html#visualIndex index and store/restore them appropriately.
    E.g. every time a section has been moved, hidden, resized etc.
    [/quote]



  • Thanks for the answers,

    I would like to store the settings at the component level eg. Subclass QTableView and add saveSettings() / restoreSettings() functions which are called when the table gets initialize/closed.

    Now if I restart the application will the setting be maintained or will it be overridden as a new instance of tableView / headerView will be created each time.

    Does anyone recommends using SQLite database for storing the column settings ?



  • I recommend you to use an .ini file or the registry of the OS.

    Remember that this settings must be restored after setting the model.



  • Thanks for the reply,

    I have another question related to the same topic, For my test case I have a tableView added to the MainWindow and I would like to save the setting when the main window is closed. I override closeEvent() for my tableView where I call saveSettings() but nothing happens.

    Then I tried with eventFilters() and checked for QEvent::Close but that too doesn't seems to work. Finally based on the QEvent::type() I figured out that QEvent::Hide is called when the mainwindow is closed. Can anyone explain this behavior ?

    Regards
    SAM


  • Moderators

    only a top-level widget will receive a close event. Since your table view is a child of the top level widget (main window) it will only receive a hide event.

    If you don't dynamically change the parent-child relationships - meaning your tableview will always stay in the same top-level widget -you can do the following:
    @
    MyTableView::MyTableView() {
    this->window()->installEventFilter(this); //Note: ensure that parent was already set here
    }

    bool MyTableView::eventFilter(QObject* watched, QEvent* event)
    {
    if( watched == this->window() && event->type() == QEvent::Close )
    {
    //save the settings
    }
    return QTableView::eventFilter(watched,event);
    }
    @



  • Works perfect.

    Thanks a lot.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.