A detailed explanation of Qt Qsettings class

Source: Internet
Author: User
Tags ini

Qt4.0 also adds a very useful class qsettings, a class provided by QT4 to read the configuration file, which provides read and write to the INI file and reads and writes to the registry on the Windows platform. As we all know, users often have some kind of need for applications, such as the size of the form, where it appears, and recently opened files, which can be implemented by the Qsettings class.

In general, settings are stored in the system, in the Windows operating system is usually the INI encrypted file or the registry of the system, and for the MAC system, generally stored in the XML file, let's take a look at its specific usage.

Read "C + + GUI programming with Qt 4, Second Edition" This book people should have encountered this question, let's take a look at its constructor:

Qsettings (const QString & Organization, const QString & application = QString (), QObject * parent = 0)    
Qset Tings (scope scope, const QString & Organization, const QString & application = QString (), QObject * parent = 0)    
qsettings (format format, scope scope, const QString & Organization, const QString & application = QString ()  , QObject * parent = 0    
qsettings (const QString & fileName, format format, QObject * parent = 0)    
qsettings (QObject * parent = 0)

When we create a Qsettings object, we typically pass two arguments, the first one is the name of the company or organization, and the second is the name of the application, and we illustrate the problem with an example:

Qsettings settings ("Yzs_think", "Application");

Company or organization name: Yzs_think, program name: Application

The values are set below (general usage):

1. Write Data:

Qpoint pos = settings.value ("pos", Qpoint). Topoint ();    
Qsize size = Settings.value ("size", qsize). Tosize ();    
Resize (size);    
Move (POS);

The first sentence is to set the position of the window to appear coordinates, the second sentence implements the window size setting, the third and fourth sentences if the settings have previously saved values for POS and size, read, if not, NULL, and use the starting value we gave it. --default value--is the case for the first time the application is run.

2, read out the information data

Qsettings settings ("Yzs_think", "Application");    
Settings.setvalue ("Pos", POS ());    
Settings.setvalue ("Size", size ());

It is noteworthy here that the name of the company that writes the data and reads the data must be the same as the application name, otherwise it will not read correctly.

Here's a specific procedure to illustrate the problem:

void Mainwindow::readsettings ()    
{    
    qsettings settings ("Yzs_think", "Application");    
    Qpoint pos = settings.value ("pos", Qpoint). Topoint ();    
    Qsize size = Settings.value ("size", qsize). Tosize ();    
    Resize (size);    
    Move (POS);    
}    

void Mainwindow::writesettings ()    
{    
    qsettings settings ("Yzs_think", "Application");    
    Settings.setvalue ("Pos", POS ());    
    Settings.setvalue ("Size", size ());    
}

Then in our program, each time the application is reopened, its window size and position are the same as before the last shutdown.

This article is from the "Late Evening" blog, please be sure to keep this source http://yiluohuanghun.blog.51cto.com/3407300/952807

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.