Results 1 to 17 of 17

Thread: saveGeometry in X11

  1. #1
    Join Date
    Jul 2007
    Posts
    39
    Thanks
    10

    Default saveGeometry in X11

    Hi all

    I am trying to remember the size and location of the mainwindow from previous session in X11. Qt documentation suggests 2 methods:

    1) To use saveGeometry and restoreGeometry. This works fine with saving the size but the position is not consistent. From what I see, the position is remembered for one closeEvent and then from the second time it moves the window to top-left corner.

    2) To save pos and size.

    Doesn't method 1) work for X11?

    Thanks
    Arjun

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: saveGeometry in X11

    Yes, it does work on X11 too.
    How do you do it? Can you show us the code?
    I do not quite understand your explanation at point 1).

    Regards

  3. #3
    Join Date
    Jul 2007
    Posts
    39
    Thanks
    10

    Default Re: saveGeometry in X11

    void writeSettings() {
    QSettings settings("Z", "X");
    settings.setValue("geometry", saveGeometry());
    }


    void readSettings() {
    QSettings settings("Z", "X");
    restoreGeometry(settings.value("geometry").toByteA rray());
    }

    The size is maintained but the position of the window is not remembered at all times. It is remembered only when the position is changed in the current execution.

    Thanks
    Arjun

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: saveGeometry in X11

    Weird enough.
    Could you examine the settings, or the QByteArray, between two sessions?

  5. #5
    Join Date
    Jul 2007
    Posts
    39
    Thanks
    10

    Default Re: saveGeometry in X11

    Sorry if this is a naive question.

    Where do they get saved? Else how to check there contents using qDebug()?

    Thanks
    Arjun

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: saveGeometry in X11

    Probably they are saved in the home directory.
    On Mac they are saved in the app directory.

    Qt Code:
    1. qDebug("%s\n", saveGeometry().constData());
    To copy to clipboard, switch view to plain text mode 

    Regards

  7. #7
    Join Date
    Jul 2007
    Posts
    39
    Thanks
    10

    Default Re: saveGeometry in X11

    qDebug("%s\n", saveGeometry().constData());
    I tried that but its printing some special characters. So I guess I need to go over each byte and print it as a character.

    Is there any other better way to check the contents?

    Thanks
    Arjun

  8. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: saveGeometry in X11

    There should be the coordinates.
    Try
    Qt Code:
    1. QString str(saveGeometry());
    2. qDebug("%s", str.toAscii().constData());
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jul 2007
    Posts
    39
    Thanks
    10

    Default Re: saveGeometry in X11

    Sorry to bother you again. But it prints the same special characters even now.

    Thanks
    Arjun

  10. #10
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: saveGeometry in X11

    Well, I just read in the docs that this doesn't work on X11( the set/restoreGeometry stuff).
    They suggest using the following:
    Qt Code:
    1. void MainWindow::readSettings()
    2. {
    3. QSettings settings("Trolltech", "Application Example");
    4. QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
    5. QSize size = settings.value("size", QSize(400, 400)).toSize();
    6. resize(size);
    7. move(pos);
    8. }
    9.  
    10. void MainWindow::writeSettings()
    11. {
    12. QSettings settings("Trolltech", "Application Example");
    13. settings.setValue("pos", pos());
    14. settings.setValue("size", size());
    15. }
    To copy to clipboard, switch view to plain text mode 


    Meaning that you must save the geometry manually.
    Should work,

    Regards

  11. The following user says thank you to marcel for this useful post:

    arjunasd (7th August 2007)

  12. #11
    Join Date
    Jul 2007
    Posts
    39
    Thanks
    10

    Default Re: saveGeometry in X11

    Marcel

    Thanks a lot.
    Still I would like to know whats being saved. Just out of curiosity. If you know a method to print out the contents of QByteArray, let me know.

    Thanks again for your help
    Arjun

  13. #12
    Join Date
    Jul 2007
    Posts
    39
    Thanks
    10

    Default Re: saveGeometry in X11

    1.
    void MainWindow::readSettings()
    2.
    {
    3.
    QSettings settings("Trolltech", "Application Example");
    4.
    QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
    5.
    QSize size = settings.value("size", QSize(400, 400)).toSize();
    6.
    resize(size);
    7.
    move(pos);
    8.
    }
    9.

    10.
    void MainWindow::writeSettings()
    11.
    {
    12.
    QSettings settings("Trolltech", "Application Example");
    13.
    settings.setValue("pos", pos());
    14.
    settings.setValue("size", size());
    15.
    }

    Even with this code, the exact same thing is happening. The size is correctly remembered. But the position is not remembered if its not changed in the current execution. So, if I change the position of the window in my current execution and then close it, it is remembered. But next execution, if I do not change the position then the window goes back to the left top of the screen.

    So, does qt only save the pos when there is a change?

    Thanks
    Arjun

  14. #13
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: saveGeometry in X11

    A safer solution is to store both pos() and size() and to restore the geometry using QWidget::resize() and move() before calling show()
    Do you restore the geometry before the window is shown?
    You could do that at the end of your main window constructor.

    Regards

  15. #14
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

  16. #15
    Join Date
    Jul 2007
    Posts
    39
    Thanks
    10

    Default Re: saveGeometry in X11

    I have found a temporary solution.

    I checked the contents on the settings in the file in my HOME directory. The size is correctly updated.

    But for the position, only when there is a change in postion from the previous execution, it is being saved. So, If I dont change the position, the saved Pos is (0,0). hence the window goes back to the top-left of the screen.

    So, as a hack I am saving the position of the window as mapToGlobal(QPoint(0, 0)). This would be the screen coordinate of the window top-left corner.

    It works good except a small down shift because of the panel in the top of the screen. This panel is just a few pixels , so as of now it is fine.

    Thanks
    Arjun

  17. #16
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: saveGeometry in X11

    But this means that it should work fine if you save pos() and size() and then restore them.
    As it is stated in the documentation, save/restoreGeometry should not be used on X11.

    Regards

  18. #17
    Join Date
    Jul 2007
    Posts
    39
    Thanks
    10

    Default Re: saveGeometry in X11

    yes. As per Qt documentation, pos() and size() must work for X11 but for some reason it does not. May be its a bug in Qt?

    I am not sure. Has anyone used it before in X11?

    Thanks
    Arjun

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.