Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store Qt gui layout and restore it

Tags:

c++

qt

I have a Qt gui application that uses dock widgets and similar items, which user can adjust themselves.

I want the layout to stay on application restart. The application already has some functions to save and load user configuration, but I have no idea how would I store a layout (positions of docks, their size etc), neither how would I restore them.

Is there any easy way of doing this? Or do I have to check the size, position and location of every single element and store it separately?

like image 295
Petr Avatar asked Oct 18 '13 08:10

Petr


People also ask

What is Qtwidgets?

Widgets are the primary elements for creating user interfaces in Qt. Widgets can display data and status information, receive user input, and provide a container for other widgets that should be grouped together. A widget that is not embedded in a parent widget is called a window.

What is a QLayoutItem?

The QLayoutItem class provides an abstract item that a QLayout manipulates. This is used by custom layouts. Pure virtual functions are provided to return information about the layout, including, sizeHint(), minimumSize(), maximumSize() and expanding().

What are layouts in Qt?

The Qt layout system provides a simple and powerful way of automatically arranging child widgets within a widget to ensure that they make good use of the available space.


1 Answers

For storing your dock windows layout you can use QMainWindow::saveState(int version) and QMainWindow::restoreState(const QByteArray &state, int version) in combination with QSettings class.

Example from Qt docs:

void MyMainWindow::closeEvent(QCloseEvent *event)
{
    QSettings settings("MyCompany", "MyApp");
    settings.setValue("geometry", saveGeometry());
    settings.setValue("windowState", saveState());
    QMainWindow::closeEvent(event);
}
like image 148
vahancho Avatar answered Sep 20 '22 12:09

vahancho