Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: 'Best' Qt strategy for loading/saving user settings (esp. for QTreeView)

  1. #1
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Question 'Best' Qt strategy for loading/saving user settings (esp. for QTreeView)

    Hi,

    When the user starts my application for the first time, I load some default settings. Then, upon exiting, I save the user settings which I reload the next time round.

    Now, I was wondering what is the best way to do this in a Qt application. I currently save the user settings by overriding closeEvent(). The idea is that the GUI is still visible at that stage which allows me to properly save the user settings.

    I am, however, facing problems with the loading of the user settings, especially with those related to a QTreeView-based widget. Basically, I have some kind of a file browser within my application and the first time the user starts my application, I would like my QTreeView-based widget to have its columns resized to their contents, but then allow the user to resize them manually if s/he so desires (and save that information in the user settings). At the same time, I want to keep track of the folder/file that is currently selected and, the next time, the user starts my application have my QTreeView-based widget point to that folder/file.

    My understanding is that in order to do this, I would need my QTreeView-based widget to be visible. This means that I clearly can't do that in the constructor of my GUI class, or even QTreeView-based widget class. I therefore thought that I could do something similar to overriding closeEvent(), i.e. override showEvent() and have a static boolean that would be used to handle the loading of the user settings. Unfortunately, that approach doesn't work with my QTreeView-based widget. I just can't get the headers to autoresize or the folder/file to be scrolled to properly. In fact, this is not quite true, I have got things to work by displaying a dialog box just before doing to the autoresizing or scrolling, and it was fine, but I clearly don't want to have to display a dialog box to get things to work. So, it seems to me that my problem is that the GUI takes some time to become visible which means that I can't properly load the user settings.

    So... what is the 'best' strategy to load/save user settings that require the GUI to be fully visible? I wish there was some kind of startEvent() method (i.e. the exact opposite of closeEvent()) which I could override...

    Cheers, Alan.

  2. #2
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: 'Best' Qt strategy for loading/saving user settings (esp. for QTreeView)

    have you looked at QSettings?

  3. #3
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: 'Best' Qt strategy for loading/saving user settings (esp. for QTreeView)

    Quote Originally Posted by schnitzel View Post
    have you looked at QSettings?
    Yes, I am already using it and finds it very convenient indeed. This is not the issue though. My issue is that there are settings which I want to keep track of and restore, and some of those settings require my application to be fully visible. To store the settings, it's simple, I just override closeEvent(), but to load them I have yet to find something that works properly. I tried showEvent(), but that doesn't work for things I need to do with QTreeView for example. So... what could work?...

  4. #4
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: 'Best' Qt strategy for loading/saving user settings (esp. for QTreeView)

    Quote Originally Posted by agarny View Post
    I tried showEvent(), but that doesn't work for things I need to do with QTreeView for example.
    Why not ?
    Is your tree built after app reached showeEvent() ?

  5. #5
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: 'Best' Qt strategy for loading/saving user settings (esp. for QTreeView)

    Quote Originally Posted by totem View Post
    Why not ?
    Good question and question to which I would very much like to get an answer!

    Quote Originally Posted by totem View Post
    Is your tree built after app reached showeEvent() ?
    Well, clearly not, since for example scrollTo() doesn't work as expected when called from within showEvent(). It seems to me that QTreeView needs a bit more time to fully initialise itself which might explain why showing a dialog box just before calling scrollTo() makes scrollTo() to work...

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: 'Best' Qt strategy for loading/saving user settings (esp. for QTreeView)

    I have got things to work by displaying a dialog box just before doing to the autoresizing or scrolling, and it was fine
    Have you tried to qApp->processEvents(); instead of displaying dialog box ?

  7. #7
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: 'Best' Qt strategy for loading/saving user settings (esp. for QTreeView)

    Quote Originally Posted by stampede View Post
    Have you tried to qApp->processEvents(); instead of displaying dialog box ?
    Yes, I did at the time, but to no avail. I am going to revisit all of that today though and will see how it goes.

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

    Default Re: 'Best' Qt strategy for loading/saving user settings (esp. for QTreeView)

    Please provide a test case where scrollTo() doesn't work as expected.
    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.


  9. #9
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: 'Best' Qt strategy for loading/saving user settings (esp. for QTreeView)

    Quote Originally Posted by wysota View Post
    Please provide a test case where scrollTo() doesn't work as expected.
    Ok, I am busy with a couple of other things right now, but will try to come up with a test case as soon as possible. Who knows, it might actually help me figuring out what is wrong with my code, if anything...

  10. #10
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: 'Best' Qt strategy for loading/saving user settings (esp. for QTreeView)

    Quote Originally Posted by agarny View Post
    It seems to me that QTreeView needs a bit more time to fully initialise
    Then try to detect the end of this loading process, and load you settings right after ?

  11. #11
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: 'Best' Qt strategy for loading/saving user settings (esp. for QTreeView)

    Quote Originally Posted by totem View Post
    Then try to detect the end of this loading process, and load you settings right after ?
    I have tried that too (by having a while loop with qApp->processEvents(); in it -- yes, it was a quick and dirty way of testing things at that stage), but to no avail. What I did, if I recall correctly was to test for QTreeView to be visible, but that didn't quite work as I expected (I seem to remember that things were hanging up, but I might have something wrong back then, can't remember for certain).

  12. #12
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: 'Best' Qt strategy for loading/saving user settings (esp. for QTreeView)

    Indeed a loop on processEvents() does not seem clean
    If you have a treeview, you must have an associated model (or you meant treewidget?)
    Try to catch a signal from this model, indicating it finished to load its data; if you don't find such signal, try to implement something equivalent. Then you read treeview settings, after its data has been updated

  13. #13
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: 'Best' Qt strategy for loading/saving user settings (esp. for QTreeView)

    Quote Originally Posted by totem View Post
    Indeed a loop on processEvents() does not seem clean
    Well, I did warn you...

    Quote Originally Posted by totem View Post
    If you have a treeview, you must have an associated model (or you meant treewidget?)
    I do indeed have an associated model (a QFileSystemModel object).

    Quote Originally Posted by totem View Post
    Try to catch a signal from this model, indicating it finished to load its data; if you don't find such signal, try to implement something equivalent. Then you read treeview settings, after its data has been updated
    I am not aware of any such signal. I did, however, try to 'play' with the expanded() signal, but again to no avail.

    Anyway, I am nearly done with what I needed to do, so I should soon be able to resume that aspect of my work.

  14. #14
    Join Date
    Apr 2010
    Location
    Almaty
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: 'Best' Qt strategy for loading/saving user settings (esp. for QTreeView)

    As i understand your problem not in settings your problem in show/hide some properties of widgets. You can do save to file (as txt) your needed widgets as boolean or in integers (with specific values) then when your program opens read them from file and show/hide needed widgets.

  15. #15
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: 'Best' Qt strategy for loading/saving user settings (esp. for QTreeView)

    Quote Originally Posted by meyrambek View Post
    As i understand your problem not in settings your problem in show/hide some properties of widgets. You can do save to file (as txt) your needed widgets as boolean or in integers (with specific values) then when your program opens read them from file and show/hide needed widgets.
    Sorry meyrambek, but I believe you misunderstood my problem. Also, I wouldn't personally recommend using a text file to save my settings. I much prefer to rely on QSettings for this.

  16. #16
    Join Date
    Apr 2010
    Location
    Almaty
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: 'Best' Qt strategy for loading/saving user settings (esp. for QTreeView)

    not as txt as xml is more good way

  17. #17
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: 'Best' Qt strategy for loading/saving user settings (esp. for QTreeView)

    Quote Originally Posted by meyrambek View Post
    not as txt as xml is more good way
    Please have a look at QSettings and you will see that there is no point in using a text file or even an XML file.

    Quote Originally Posted by wysota View Post
    Please provide a test case where scrollTo() doesn't work as expected.
    Ok, here goes for a very simple example of what I am trying to do with scrollTo():

    QTreeView.pro:
    Qt Code:
    1. QT += core gui
    2.  
    3. TARGET = QTreeView
    4. TEMPLATE = app
    5.  
    6. SOURCES += main.cpp
    To copy to clipboard, switch view to plain text mode 
    main.cpp:
    Qt Code:
    1. #include <QApplication>
    2. #include <QFileSystemModel>
    3. #include <QHeaderView>
    4. #include <QTreeView>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9.  
    10. QFileSystemModel fsm;
    11.  
    12. fsm.setRootPath(""); // I.e. access to the whole file system
    13.  
    14.  
    15. w.setModel(&fsm);
    16. w.header()->setResizeMode(QHeaderView::ResizeToContents);
    17.  
    18. w.show();
    19.  
    20. QModelIndex mi = fsm.index("C:\\Windows\\System32\\zipfldr.dll");
    21.  
    22. w.setCurrentIndex(mi);
    23. w.scrollTo(mi);
    24.  
    25. return a.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 
    The idea behind line 21 is that we point to a file which is out of the field of view and should therefore get scrollTo() to scroll down to that file. Unfortunately, this doesn't work for me, as shown below:

    QTreeView_scrollTo.png
    Last edited by agarny; 22nd February 2011 at 13:31.

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

    Default Re: 'Best' Qt strategy for loading/saving user settings (esp. for QTreeView)

    I would guess the model index might simply be invalid because the filesystem model takes time to populate itself. Have you tried that with a different model?
    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.


  19. #19
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: 'Best' Qt strategy for loading/saving user settings (esp. for QTreeView)

    Quote Originally Posted by wysota View Post
    I would guess the model index might simply be invalid because the filesystem model takes time to populate itself. Have you tried that with a different model?
    Sorry, are you saying that mi doesn't contain the right information? If so, then no it does contain the right information, since if I manually scroll down, then I will see the right file selected (as a result of w.setCurrentIndex(mi)).

    Otherwise, what do you mean trying with another model? Do you mean something else than QFileSystemModel?

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

    Default Re: 'Best' Qt strategy for loading/saving user settings (esp. for QTreeView)

    Quote Originally Posted by agarny View Post
    Do you mean something else than QFileSystemModel?
    Yes, that's what I mean.

    This works as expected:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char **argv){
    4. QApplication app(argc, argv);
    5. QListView view;
    6. for(int i=1;i<=200;++i){
    7. list << QString("Item %1").arg(i);
    8. }
    9. model.setStringList(list);
    10. view.setModel(&model);
    11. view.show();
    12. view.setCurrentIndex(model.index(100,0));
    13. view.scrollTo(model.index(100,0));
    14. return app.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 
    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. Loading and saving in-memory SQLITE databases
    By miraks in forum Qt Programming
    Replies: 10
    Last Post: 27th April 2010, 21:24
  2. XML saving settings
    By ^NyAw^ in forum Qt Programming
    Replies: 12
    Last Post: 12th May 2009, 17:05
  3. Saving settings to XML
    By arturo182 in forum Qt Programming
    Replies: 2
    Last Post: 8th March 2009, 11:10
  4. QDataStream and saving and loading QList
    By Noxxik in forum Qt Programming
    Replies: 3
    Last Post: 1st March 2009, 22:02
  5. saving settings does not work
    By MarkoSan in forum Qt Programming
    Replies: 4
    Last Post: 13th June 2008, 19:29

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.