3、Qt--配置文件的使用

开发平台:Win10 64位

开发环境:Qt Creator 13.0.0 

构建环境:Qt 5.15.2 +MSVC2019 64位

一、需求及方案

        实际开发过程中,我们需要根据本地的配置文件,去配置我们的程序,比如数据库地址、网络地址等信息,而配置文件的格式一般是.ini、.cfg或者.properties类型的,通过Qt官方提供的QSettings类,就可以实现我们的需求,简化了我们开发的难度!

二、实操

.ini文件可以看作是键值对的集合,在文件中以[section]来表示一个区域,然后在该区域中定义多个键值对,格式如下:

[section]

key1=value1

key2=value2

...

其中[section]表示一个区域,比如说[mysql],就表示是mysql的配置信息,下面使用QSrttings类来读取和写入ini文件。

首先我们需要明确配置文件中有什么,这样在初始化的时候就可以直接生成,不用手动去创建配置文件。

#ifndef CONFIGFILE_H
#define CONFIGFILE_H

#include <QObject>

class configfile
{
public:
    configfile();
    //需要一个文件名,不用设置保存路径,会自动生成在exe同级目录下
    static QString configFileName;
    //需要用到的配置变量
    static QString Account;
    static QString password;

    static QString remoteIp;
    static int remotePort;

    //方法 读写
    static bool checkConfigFile();
    static void newConfigFile();

    static void readConfigFile();
    static void writeConfigFile();

};

#endif // CONFIGFILE_H
#include "configfile.h"
#include <QFileInfo>
#include <QSettings>

configfile::configfile() {}

QString configfile::configFileName="test.ini";
QString configfile::Account="admin";
QString configfile::password="admin";
QString configfile::remoteIp="127.0.0.1";
int configfile::remotePort=502;

//首先检擦下存不存在文件,如果不存在就新建,存在就读写操作
bool configfile::isConfigFileExist()
{

    QFileInfo fileInfo(configfile::configFileName);
    if(fileInfo.isFile())
    {
        //存在情况下,看看内容全不全
        QFile file(configfile::configFileName);
        if (file.size() == 0) {
            configfile::newConfigFile();
            return false;
        }
        //如果配置文件不完整,则以初始值继续运行,并生成配置文件
        if (file.open(QFile::ReadOnly)) {
            bool ok = true;
            while (!file.atEnd()) {
                QString line = file.readLine();
                line = line.replace("\r", "");
                line = line.replace("\n", "");
                QStringList list = line.split("=");

                if (list.count() == 2) {
                    if (list.at(1) == "") {
                        ok = false;
                        break;
                    }
                }
            }

            if (!ok) {
                configfile::newConfigFile();
                return false;
            }
        } else {
            configfile::newConfigFile();
            return false;
        }
        return true;
    }else {
        configfile::newConfigFile();
        return false;
    }
}


//新建配置文件
void configfile::newConfigFile()
{
    writeConfigFile();
}

//读取配置文件
void configfile::readConfigFile()
{
    //先判断配置文件是否存在
    if (!isConfigFileExist()) {
        return;
    }

    QSettings set(configfile::configFileName, QSettings::IniFormat);
    set.setIniCodec("utf-8");
    set.beginGroup("mysql");
    configfile::Account = set.value("Account",configfile::Account).toString();
    configfile::password = set.value("password",configfile::password).toString();
    set.endGroup();

    set.beginGroup("net");
    configfile::Account = set.value("Account",configfile::Account).toString();
    configfile::password = set.value("password",configfile::password).toString();
    set.endGroup();
}

//写入配置文件
void configfile::writeConfigFile()
{
    //下面这段应该会自动生成配置文件?
    QSettings set(configfile::configFileName, QSettings::IniFormat);
    set.beginGroup("mysql");
    set.setValue("Account",configfile::Account);
    set.setValue("password",configfile::password);
    set.endGroup();

    set.beginGroup("net");
    set.setValue("remoteIp",configfile::remoteIp);
    set.setValue("remotePort",configfile::remotePort);
    set.endGroup();
}

三、参考文献

3.1 Qt判断文件、文件夹是否存在,不存在则创建文件夹。_qt 文件不存在则创建-CSDN博客

3.2 Qt基础之配置文件(QSettings) - 浅笑19 - 博客园 (cnblogs.com)

  • 11
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Ubuntu上配置Qt-arm交叉编译环境,您需要执行以下步骤: 1. 安装交叉编译工具链 您可以使用以下命令安装交叉编译工具链: ``` sudo apt-get install gcc-arm-linux-gnueabi g++-arm-linux-gnueabi ``` 2. 下载Qt 您可以从Qt官网下载Qt for Embedded Linux的源代码。解压缩下载的文件并将其放在您选择的目录中。 3. 配置Qt 进入解压后的Qt目录,执行以下命令: ``` ./configure -xplatform qws/linux-arm-gnueabi-g++ -embedded arm -little-endian -prefix /usr/local/qt-arm -no-largefile -no-accessibility -no-qt3support -no-sql-sqlite -no-glib -no-cups -no-pch -no-mmx -no-3dnow -no-sse -no-sse2 -no-ssse3 -no-sse3 -no-sse4.1 -no-sse4.2 -no-avx -no-neon -no-openvg -no-gtkstyle -no-nis -no-iconv -no-dbus -no-opengl -no-rpath -no-gif -no-libtiff -no-libpng -no-libmng -no-libjpeg -no-openssl -no-nas-sound -no-pulseaudio -no-alsa -no-xinerama -no-xcursor -no-xfixes -no-xrandr -no-xrender -no-xinput -no-xkb -no-sm -no-xshape -no-xvidmode -no-xinerama -no-xcursor -no-xfixes -no-xrandr -no-xrender -no-xinput -no-xkb -no-sm -no-xshape -no-xvidmode ``` 请注意,此命令假定您已经安装了arm交叉编译工具链,并且将Qt安装到了/usr/local/qt-arm目录中。 4. 编译Qt 执行以下命令编译Qt: ``` make ``` 这将需要一些时间来完成。 5. 安装Qt 执行以下命令安装Qt: ``` sudo make install ``` 6. 配置环境变量 将以下行添加到您的~/.bashrc文件中: ``` export PATH=/usr/local/qt-arm/bin:$PATH ``` 这将使您能够在命令行中轻松访问Qt。 完成以上步骤后,您应该已经成功配置了Qt-arm交叉编译环境。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值