Qt ini配置文件

ini文件用于保存用户的设置操作,下列以背景颜色设置为例子

暂时默认设置为白色背景

这段代码放置在主窗口的构造函数中,用于初始化读取ini文件

cpp 复制代码
    QString color;
    //第一个参数在我这里ini文件时相对路径需要放在工程中,也可以写绝对路径
    QSettings *set = new QSettings("color.ini",QSettings::IniFormat);
    set->beginGroup("backgroundcolor");
    color = set->value("color","").toString();
    set->endGroup();
    delete  set;

    this->setStyleSheet(QString("background:'%1'").arg(color));

对应点击按钮的槽函数:

cpp 复制代码
void MainWindow::on_pushButton_clicked()
{
    QColor color;
    color = QColorDialog::getColor(Qt::white,this," Choose color",QColorDialog::ShowAlphaChannel);
    
    if(color.isValid()){  //如果颜色无效则不保存。
        this->setStyleSheet(QString("background:'%1'").arg(color.name()));

        QSettings *set  = new QSettings("color.ini",QSettings::IniFormat);
        set->beginGroup("backgroundcolor");
        set->setValue("color",color.name());
        set->endGroup();
        delete  set;
    }
}

color.isValid()函数用以解决当用户在对话框中没有选择Cancel 或者直接关闭窗口时,变为读取ini文件时,变为全黑色。

打开对话框选择为绿色后:

此时ini文件内容为:

相关推荐
四维碎片17 小时前
【Qt】配置安卓开发环境
android·开发语言·qt
西游音月17 小时前
(7)框架搭建:Qt实战项目之主窗体导航栏、状态栏
开发语言·qt
万象.18 小时前
QT基础及对象树的认识
c++·qt
柒儿吖18 小时前
Qt for HarmonyOS 水平进度条组件开发实战
开发语言·qt·harmonyos
应用市场1 天前
Qt QTreeView深度解析:从原理到实战应用
开发语言·数据库·qt
864记忆1 天前
Qt Widgets 模块中的函数详解
开发语言·qt
彡皮1 天前
基于Qt,调用千问7B大模型,实现智能对话
开发语言·qt·大模型·千问7b
qq_401700411 天前
QT的5种标准对话框
开发语言·qt
rainFFrain1 天前
qt显示类控件---QCalendarWidget
开发语言·qt
爱奥尼欧1 天前
【QT笔记】常用控件——QWidget 核⼼属性
数据库·笔记·qt