Restoring Qt Application State and Geometry: Part 1

You have probably used an application that restores state and/or geometry at some point. State means how things “are” in an application, to so sum it up. Geometry is where the window was on the screen, and the size. We’ll be using some Qt functions that take care of most of the actual settings work. I’ll also show you how to override the closing event of a QMainWindow so you can inject custom code into the virtual event. For example, say you are making a text editor and you want to save the current files that are open. In addition, you’ll learn a few other things along the way, while the main focus will be QSettings.

So without further ado, let us begin!


Note: All code in this tutorial series is released under the MIT License. This blog’s copyright notice still applies — all code is copyright Contingency Coder’s Blog. 

First, open up Qt Creator and create a new project. I’ll be calling mine “WindowStates”. Set up your files how you prefer, however, I am leaving the MainWindow and other files the same. And great, you’re done! Lets start with the main.cpp file:

#include "mainwindow.h"
#include <QApplication>

// For Fusion Style
#include <QStyleFactory>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    // Set the application's style to Fusion
    QApplication::setStyle(QStyleFactory::create("Fusion"));

    // Use your real info here. This is the information QSettings
    // will use to store our settings later.
    QApplication::setOrganizationDomain("com.wordpress.contigencycoder");
    QApplication::setOrganizationName("Contingency Bloggers Corporation");
    QApplication::setApplicationName("Save State");
    QApplication::setApplicationVersion("1.0.0");

    // Create MainWindow and show it...
    MainWindow window;
    window.show();

    // Start out app's event loop
    return app.exec();
}

Lets look at this file in more detail. First, we include the necessary files. QStyleFactory’s purpose is to provide us the style factory for the Fusion style. Next we add these lines:

QApplication::setOrganizationDomain("com.wordpress.contigencycoder");
QApplication::setOrganizationName("Contingency Bloggers Corporation");
QApplication::setApplicationName("Save State");
QApplication::setApplicationVersion("1.0.0");

These lines control how the settings we write are stored. You should always use unique identification, or you could conflict with other applications. For example, do not put “com.google” in your Organization Domain… that is, if you aren’t Google…

After that, we simply create our MainWindow instance, and show it. Then we launch our applications event loop with app.exec(). In the next tutorial, we will take a look at the preliminary MainWindow class and eventually getting the state and geometry to save.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s