Results 1 to 5 of 5

Thread: Restoring Widget State--"Best Practices"?

  1. #1
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Restoring Widget State--"Best Practices"?

    I use QSettings to save and restore the state of things like checkboxes, radioboxes and the currentIndex of comboboxes.

    I set the state of these in an owning widgets constructor, using QSetting::getValue(). I connect a signal like toggled(bool) from each widget to a private slot with code that does nothing other than a QSetting::setValue, based on the boolean passed by the toggled(bool) value.

    So, I end up having to create a trivial member function for each widget, that does nothing other than keep the QSetting consistent with the current state of the widget.

    I could only save the settings on application exit, but it's very nice to be able to get all the widgets back to a saved state when things like exceptions occur (during code development). So, I've fallen into the habit of keeping the QSetting updated every time the widget changes.

    Is this a bad practice? Also, saving the state of something simple like a checkbox involves:

    1) Connect statement in constructor (toggled(bool) to trivial slot function.
    2) Header file update for the private slot.
    3) cpp file update for slot implementation.

    Is there an easier/better way?

    Thanks,

    Dave Thomas
    Last edited by davethomaspilot; 14th January 2013 at 13:23. Reason: Subscribe to thread

  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: Restoring Widget State--"Best Practices"?

    For a couple of checks/radio buttons this is probably the most obvious way. For a larger number you could make use of the QObject object name to hold the name of the settings key for the check box and then use that in a single servicing slot. Here is an example with generated keys but you can set them manually (in Designer for example):

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class LotsOfChecks: public QWidget {
    4. Q_OBJECT
    5. public:
    6. LotsOfChecks(QWidget *p = 0): QWidget(p) {
    7.  
    8. QVBoxLayout *layout = new QVBoxLayout(this);
    9. setLayout(layout);
    10. for (int i = 0; i < 10; ++i) {
    11. QCheckBox *cb = new QCheckBox("Some cool label", this);
    12. cb->setObjectName(QString("Check_%1").arg(i));
    13. connect(cb, SIGNAL(toggled(bool)), SLOT(storeCheck(bool)));
    14. layout->addWidget(cb);
    15. }
    16. restoreChecks();
    17. }
    18. private:
    19. void restoreChecks() {
    20. QSettings settings;
    21. foreach(const QString &key, settings.childKeys()) {
    22. QCheckBox *cb = findChild<QCheckBox*>(key);
    23. if (cb)
    24. cb->setChecked(settings.value(key).toBool());
    25. }
    26. }
    27.  
    28. private slots:
    29. void storeCheck(bool checked) {
    30. QString key = sender()->objectName();
    31.  
    32. QSettings settings;
    33. settings.setValue(key, checked);
    34. }
    35. };
    36.  
    37. int main(int argc, char **argv)
    38. {
    39. QApplication app(argc, argv);
    40. app.setApplicationName("thisapp");
    41. app.setOrganizationName("com.example");
    42.  
    43. LotsOfChecks w;
    44. w.show();
    45.  
    46. return app.exec();
    47. }
    48. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Restoring Widget State--"Best Practices"?

    I would suggest a generic approach that involves querying each widget for the user property, connecting to that property changes and storing them in settings.

    It can all be done with QMetaObject, QMetaProperty and QMetaMethod. Pseudocode:

    1. check widget's USER property by asking QMetaObject::userProperty()2. check the notify method for this property using QMetaProperty::notifySignal()3. chek the signal signature with QMetaMethod::signature()4. connect to that signal with a custom slot taking no arguments
    5. recursively do the same for all child widgets you want to service

    In the custom slot simply ask the sender() for its USER property again, read its value and store along the object name of the widget.

    For restoring the state, do the same just adding a step to set the property value before step 4.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Restoring Widget State--"Best Practices"?

    Thanks for taking the time for this reply, complete with example code! This seems much better than what I've been doing.

    Is this a common way of doing this, or do most developers do it in the more straight forward (but a bit tedious) way?


    Added after 11 minutes:


    Previous reply was to ChrisW67.

    Wysota, if I understand correctly, use of the "Meta" classes with this technique will restore the state of ALL widget types?

    Same question I had for Chris. Is this a commonly used techinque? If so, are there example code fragments somewhere?

    Thank you both for taking the time to reply. This forum has saved me many hours of time and is helping get on board with Qt, and inspiring me to invest in learning how to "do it right" instead of "just getting it done".
    Last edited by davethomaspilot; 15th January 2013 at 12:10.

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

    Default Re: Restoring Widget State--"Best Practices"?

    Quote Originally Posted by davethomaspilot View Post
    Wysota, if I understand correctly, use of the "Meta" classes with this technique will restore the state of ALL widget types?
    Yes, all that have a USER property defined (I think all built-in widgets do). You just have to be aware not to overdo it -- e.g. if you have a QCalendarWidget then it consists of a number of other widgets. You don't want to recurse from QCalendarWidget to its children, it's enough to use the USER property QCalendarWidget defines. Hence you have to provide stop conditions for complex widgets.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 2
    Last Post: 2nd March 2012, 07:55
  2. Replies: 1
    Last Post: 7th April 2010, 21:46
  3. Replies: 3
    Last Post: 17th March 2010, 16:47
  4. how to check button state such as "hover"?
    By billconan in forum Qt Programming
    Replies: 2
    Last Post: 12th November 2009, 01:45
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

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
  •  
Qt is a trademark of The Qt Company.