[Solved] Saving sizes of Windows & Dialogs?
-
Hi,
I am currently saving the window sizes for all my windows & dialogs in a preferences file.
On startup I am loading this file & resizing all the windows by using the saved values in this file.
However I am finding it hard to manage due to large no of windows.So does Qt have any way of saving all the window & dialog sizes from the previous session & then using them automatically whenever the application is launched next time?
-
no there is no auto-magic feature available. :)
You will have to do this by your own. This isn't even possible in every case.
E.g. some windows are opened during run time and thus may not even be created on start-up. Or the top-level widgets may not identifiable in all cases. It's just not trivial...About what windows do we talk? QMainWindow::saveState() saves the psoition of all toolbars and QDockWidgets (when they have a unique objectname set).
A possible solution could be achieved with multiple-inheritance. You create a class which restores the state in the constructor (or on showEvent()) and saves the state in the destrucutor (or closeEvent()).
But this would involve that you subclass all widgets you do want to use and inherit from this class.Or you create a wrapper QObject which "manages" the widget and installs itself as eventFilter on the widget. Probably this would be the best solution.
@
class WindowSaveRestoreManager : public QObject
{
Q_OBJECT
public:
explicit WindowSaveRestoreManager(QWidget* window) : m_Window(window) {
connect( window, SIGNAL(destroyed()), this, SLOT(deleteLater()) );
window->installEventFilter(this);
}virtual bool eventFilter(QObject* obj, QEvent* event) { if( obj == m_Window ) { switch( event->type() ) { case QEvent::Show; //restore geom break; case QEvent::Close; //save geom break; } } return false; }
protected:
QPointer<QWidget> m_Window;
}
@But like QMainWindow::saveState() you will also need some identifier on the widget in order to restore the correct state.
-
Hi raven & prady,
Thanks for your useful inputs.
I am thinking of a way by combining both of your methods.
I will save the positions & sizes of windows & dialogs in the registry using QSettings.
I will maintain a SLOT which will be connected to the close() SIGNAL of these windows & update the registry by taking values from these windows.
On startup (@raven I meant my application's startup & not Windows startup) I can then refer to the values from registry & resize accordingly.
I already have a static class which maintains some user preferences, this would come in handy.Any suggestions or warnings?
-
Suggestions:
0) If your application is meant for users with low privileges (not admins), then you might want to completely avoid the registry since it requires the user to give administrative privileges to the application. If the user fails to agree with that (either because he doesn't have the admin password or chooses not to do so in the dialog) your application will not be able to read the settings from the registry and you would need a fail-safe implementation. Instead, use a simple settings file (INI or XML files are perfectly suited for the task).
1) You might only want to remember the position and size of some important windows; for example, dialog windows and other temporary ones (like "do you want to do that? [YES][SURE][OF COURSE]") are perfectly fine if displayed randomly.
2) The registry can be a good way to store settings but the more keys you access the more time it will take to load all the settings. If you want a fast application loading you might want to store the position and size of the windows in the same key (like: "window1{0,0,200,150},window2{30,50,100,100},window3{300,450,25,25}") so only 1 access to the registry will give you all the settings. If you load the settings after starting the application it might not be a noticeable slowdown time.
Warnings? Yes... it's the Windows registry, so, don't mess with it more than Windows already does... :P -
Hi T3STY,
Thanks for your input.
I am now totally avoiding Windows registry.Yes in all there 5 Windows whose geometry I want to remember.
I was already maintaining a preferences file.
Now I am putting these details in it also.Thanks for your guidance again : )