Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

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

    General and Desktop
    3
    9
    8543
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      Sam last edited by

      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,

      1 Reply Last reply Reply Quote 0
      • francescmm
        francescmm last edited by

        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.

        1 Reply Last reply Reply Quote 0
        • raven-worx
          raven-worx Moderators last edited by

          [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.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply Reply Quote 0
          • francescmm
            francescmm last edited by

            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]

            1 Reply Last reply Reply Quote 0
            • S
              Sam last edited by

              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 ?

              1 Reply Last reply Reply Quote 0
              • francescmm
                francescmm last edited by

                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.

                1 Reply Last reply Reply Quote 0
                • S
                  Sam last edited by

                  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

                  1 Reply Last reply Reply Quote 0
                  • raven-worx
                    raven-worx Moderators last edited by

                    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);
                    }
                    @

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    1 Reply Last reply Reply Quote 0
                    • S
                      Sam last edited by

                      Works perfect.

                      Thanks a lot.

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post