[置顶] QT 使用QSettings 读写ini配置文件

qt 中如果想要保存参数,可选用QSettings类生成.ini文件保存参数

源程序:


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

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QSettings setting("/home/qust/qt/setting/config.ini",QSettings::IniFormat);// 读配置文件
    setting.beginGroup("config");
    QString STR=setting.value("page").toString();
    ui->textBrowser->insertPlainText(STR);
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(pushButton_clicked()));

}

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

void MainWindow::pushButton_clicked()

{   QString string=ui->textEdit->toPlainText();
    ui->textBrowser->insertPlainText(string);
    QSettings setting("/home/qust/qt/setting/config.ini",QSettings::IniFormat);/ /生成配置文件
        setting.beginGroup("config");// beginGroup与下面endGroup 相对应,“config”是标记
           setting.setValue("page",QVariant(string));
           setting.setValue("site",QVariant("http://www.cppblog.com/gaimor/"));
           setting.setValue("maker",QVariant("Gaimor"));
        setting.endGroup();
}

运行,在textEdit输入successfull,打开。.ini文件,

[config]
page=ddddddd
site=http://www.cppblog.com/gaimor/
maker=Gaimor


完成,退出程序,下次再运行,就能读取上次的参数了

你可能感兴趣的:(UI,String,delete,ini,qt,Signal)