Results 1 to 4 of 4

Thread: Saving state of nested widgets

  1. #1
    Join Date
    Jan 2014
    Posts
    36
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Saving state of nested widgets

    My application has the following setup: on the main window (QMainWindow), I've got a tabbed widget (QTabWidget), which the user can configure to have 1-N tabs open. On each tab is a container widget which the user can configure to hold 1-M plot items (QwtPlot). While using the application, the user can choose what data fields are displayed on each plot from a list of available data feeds. This allows the user to create a custom setup of number of tabs, number of plots per tab, and custom data on each plot.

    I'd like to be able to allow the user to be able to export/import that information (number of tabs, number of plots per tab, subscribed data fields per plot), so that they can create repeatable configurations.

    There's two requirements for this:
    1. When the user closes the application, the current state is saved and is then restored on the next application launch
    2. The user can export the setup explicitly to a file and restore the state from a file. For example, the user can export one setup as "MySetup1" and export a different setup as "MySetup2", then read back in "MySetup1" and have the settings go back to that setup.


    So the first question is: should I do all this with one (or maybe two?) QSettings objects? The application will be running mainly on Windows, so in requirement #1 (saving/restoring previous state), the settings should be stored in the registry, which leads to creating a QSettings object that is using QSettings::NativeFormat. But then for requirement #2 (import/export of custom setups), I think I need to use a second QSettings object created with QSettings(const QString & fileName, Format format, QObject * parent = 0) with Format set to QSettings::IniFormat.

    The second question, assuming QSettings is the right class for this, what's the correct way to save out this info? I think I need to do something like the following:
    Qt Code:
    1. settings->setValue("NumTabs", ui->tabPlotWidget->count());
    2. for (int i=0; i < ui->tabPlotWidget->count(); i++)
    3. {
    4. settings->beginGroup(QString("Tab") + QString::number(i));
    5. settings->setValue("NumPlots",ui->tabPlotWidget->widget(i)->plotCount());
    6. for (int j=0; j < ui->tabPlotWidget->widget(i)->plotCount(); j++)
    7. {
    8. settings->beginGroup(QString("Plot") + QString::number(j));
    9. // write out all plot information
    10. settings->endGroup(); // end the "Plot#" group
    11. }
    12. settings->endGroup(); // end the "Tab#" group
    13. }
    To copy to clipboard, switch view to plain text mode 

    And then do the reverse for reading it back in... Any better ideas?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Saving state of nested widgets

    You can use any kind of file format, even a custom one, if you find something more applicapable than key value pairs (QSettings),

    The first requirement just implies that the application knows which filename you are using for the restore file.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2014
    Posts
    36
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Saving state of nested widgets

    True, I guess I should have been a little more specific. On the surface, it seems like QSettings fits the bill for what I'm trying to do, I guess I just wanted to hear arguments one way or the other about benefits/limitations choosing QSettings over a custom file format.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Saving state of nested widgets

    If using QSettings doesn't make the code awkward then go for it.

    I would still recommend to use the same format for both explicitly saved state as well as end-of-program state, e.g. with QSettings always use IniFormat, and just remember the file name for the end-of-program file using native format.

    This way you have exactly the same code paths, don't have any difference in backend capability and can easily switch to a different presistence technology if QSettings becomes too limited.

    Cheers,
    _

Similar Threads

  1. mouse events and nested widgets
    By tuli in forum Qt Programming
    Replies: 9
    Last Post: 23rd November 2012, 15:56
  2. turning widgets to original state
    By helvin in forum Qt Programming
    Replies: 2
    Last Post: 21st September 2009, 08:35
  3. saving the state of the ui
    By zgulser in forum Qt Programming
    Replies: 3
    Last Post: 24th March 2009, 12:17
  4. Saving state of connections
    By Chuk in forum Qt Programming
    Replies: 11
    Last Post: 26th August 2007, 17:01
  5. [QT4] Saving a widget state
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 1st May 2006, 14:31

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.