PDA

View Full Version : saveGeometry in X11



arjunasd
7th August 2007, 21:56
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

marcel
7th August 2007, 22:00
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

arjunasd
7th August 2007, 22:04
void writeSettings() {
QSettings settings("Z", "X");
settings.setValue("geometry", saveGeometry());
}


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

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

marcel
7th August 2007, 22:21
Weird enough.
Could you examine the settings, or the QByteArray, between two sessions?

arjunasd
7th August 2007, 22:25
Sorry if this is a naive question.

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

Thanks
Arjun

marcel
7th August 2007, 22:28
Probably they are saved in the home directory.
On Mac they are saved in the app directory.



qDebug("%s\n", saveGeometry().constData());


Regards

arjunasd
7th August 2007, 22:32
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

marcel
7th August 2007, 22:35
There should be the coordinates.
Try


QString str(saveGeometry());
qDebug("%s", str.toAscii().constData());

arjunasd
7th August 2007, 22:54
Sorry to bother you again. But it prints the same special characters even now.

Thanks
Arjun

marcel
7th August 2007, 22:58
Well, I just read in the docs that this doesn't work on X11( the set/restoreGeometry stuff).
They suggest using the following:


void MainWindow::readSettings()
{
QSettings settings("Trolltech", "Application Example");
QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
QSize size = settings.value("size", QSize(400, 400)).toSize();
resize(size);
move(pos);
}

void MainWindow::writeSettings()
{
QSettings settings("Trolltech", "Application Example");
settings.setValue("pos", pos());
settings.setValue("size", size());
}




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

Regards

arjunasd
7th August 2007, 23:01
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

arjunasd
7th August 2007, 23:09
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

marcel
7th August 2007, 23:14
A safer solution is to store both pos() (http://www.qtcentre.org/forum/qwidget.html#pos-prop) and size() (http://www.qtcentre.org/forum/qwidget.html#size-prop) and to restore the geometry using QWidget::resize (http://www.qtcentre.org/forum/qwidget.html#size-prop)() and move() (http://www.qtcentre.org/forum/qwidget.html#pos-prop) before calling show() (http://www.qtcentre.org/forum/qwidget.html#show)

Do you restore the geometry before the window is shown?
You could do that at the end of your main window constructor.

Regards

marcel
7th August 2007, 23:35
http://doc.trolltech.com/4.3/geometry.html#restoring-a-window-s-geometry

arjunasd
7th August 2007, 23:44
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

marcel
8th August 2007, 00:39
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

arjunasd
8th August 2007, 00:56
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