The use of QT's QSetting

During the use of the product, some users have specific habits. For example, the size of the window, the way the file is opened, and so on.

Therefore, we need to have a file to save some user preference information, and then set the preference when the program restarts.

The magic QSetting class is used here.

This class allows you to create a configuration file or manipulate the registry, you can also specify the location of the configuration file, of course, reading is no problem.

The following is an example of creating a configuration file and reading the configuration file. The function is to write the size of the main window, and then change the default size of the window at the next startup. Of course, this is only used very mechanically, and some modifications need to be made in actual development. Mainly look at the example...

The code is as follows (remember to read the comments):

  1. QSettings setting("C:/Users/linuxer/Desktop/i.ini",QSettings::IniFormat);//设置配置文件的目录和位置,如果有,则不动,没有,会自动创建

  2. if(setting.contains(tr("AboutSize/myQtWidth"))&&setting.contains(tr("AboutSize/myQtHeight")))//如果已经存在这个文件,那就进行读取

  3. {

  4. double width=setting.value("AboutSize/myQtWidth").toDouble();//将读取出的数据进行使用

  5. double height=setting.value("AboutSize/myQtHeight").toDouble();

  6. this->resize(width,height);

  7. this->setWindowTitle("已经成功执行");

  8. }

  9. else {

  10. setting.beginGroup(tr("AboutSize"));//节点开始

  11. setting.setValue("myQtWidth","600");//设置key和value,也就是参数和值

  12. setting.setValue("myQtHeight","500");

  13. setting.endGroup();//节点结束

  14. }

 

Guess you like

Origin blog.csdn.net/qq_14874791/article/details/109259263