Qt profile ini operations

Introduction

 When people use the program, there will be some common usual operation, every time you open the program will have to do it again these operations very cumbersome. Profiles can record some common set of information, so you do not every time you open the program, go reconfigure them.
 ini, Initialization File, the windows are stored in the format used by the system configuration file.

Qt simple to use ini

step

  1. Create Object
	QSettings setting(filePath, fileFormat)
  1. Add key-value pair (key-value)
	setting.setValue(key, value);
  1. By reading the key value
	setting.value(key);

Note: To include sub-key is also very simple requiring only key among the inputted character string, a backslash '\' separator line. On Windows, '\' is converted into QSettings '/', which makes them identical.

	setting.setValue("parentKey/childKey", "5");

In this way, we will see in the configuration,

	[parentKey]
	childKey=5

Code

mainwindows.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H


#include <QMainWindow>
#include <QSettings>
namespace Ui {
class MainWindow;
}


class MainWindow : public QMainWindow
{
    Q_OBJECT


public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();


private slots:
    void on_pushButton_add_clicked();


    void on_pushButton_read_clicked();


private:
    Ui::MainWindow *ui;
    QSettings *m_settings;
};


#endif // MAINWINDOW_H


mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSettings>
#include <QDebug>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_settings = new QSettings("./config.ini", QSettings::IniFormat);
}


MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_add_clicked()
{
    m_settings->setValue(ui->lineEdit_key->text(), ui->lineEdit_value->text().toInt());
}


void MainWindow::on_pushButton_read_clicked()
{
    ui->textBrowser->append(QString("%1:%2").arg(ui->lineEdit_key->text()).arg(m_settings->value(ui->lineEdit_key->text()).toString()));
}


mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>497</width>
    <height>367</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <layout class="QHBoxLayout" name="horizontalLayout">
    <item>
     <layout class="QVBoxLayout" name="verticalLayout_display">
      <item>
       <widget class="QTextBrowser" name="textBrowser"/>
      </item>
     </layout>
    </item>
    <item>
     <layout class="QGridLayout" name="gridLayout_input">
      <item row="4" column="1">
       <spacer name="verticalSpacer_2">
        <property name="orientation">
         <enum>Qt::Vertical</enum>
        </property>
        <property name="sizeHint" stdset="0">
         <size>
          <width>20</width>
          <height>40</height>
         </size>
        </property>
       </spacer>
      </item>
      <item row="0" column="1">
       <widget class="QLineEdit" name="lineEdit_key">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
          <horstretch>0</horstretch>
          <verstretch>0</verstretch>
         </sizepolicy>
        </property>
       </widget>
      </item>
      <item row="2" column="0">
       <widget class="QPushButton" name="pushButton_add">
        <property name="text">
         <string>添加</string>
        </property>
       </widget>
      </item>
      <item row="1" column="0">
       <widget class="QLabel" name="label_value">
        <property name="text">
         <string>数据(value)</string>
        </property>
       </widget>
      </item>
      <item row="1" column="1">
       <widget class="QLineEdit" name="lineEdit_value">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
          <horstretch>0</horstretch>
          <verstretch>0</verstretch>
         </sizepolicy>
        </property>
       </widget>
      </item>
      <item row="4" column="0">
       <spacer name="verticalSpacer">
        <property name="orientation">
         <enum>Qt::Vertical</enum>
        </property>
        <property name="sizeHint" stdset="0">
         <size>
          <width>20</width>
          <height>40</height>
         </size>
        </property>
       </spacer>
      </item>
      <item row="0" column="0">
       <widget class="QLabel" name="label_key">
        <property name="text">
         <string>键值(key)</string>
        </property>
       </widget>
      </item>
      <item row="3" column="0">
       <widget class="QPushButton" name="pushButton_read">
        <property name="text">
         <string>读取配置</string>
        </property>
       </widget>
      </item>
     </layout>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>497</width>
     <height>23</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>


ui
layout

effect

play
Profiles

summary

 A simple application demo, not very standard, but enough to use desktop app. QSettings this category actually includes a number of features, including cross-platform features, but due to the need to use, and the application itself is a type of class, so do not do start speaking.

reference

  • Assistant 5.13.0 (MinGW 7.3.0 64-bit)
Published 60 original articles · won praise 18 · views 20000 +

Guess you like

Origin blog.csdn.net/BadAyase/article/details/103871804