一、使用总结

1.配置文件生成在exe目录下。

二、代码

#ifndef CONFIGURE_H
#define CONFIGURE_H
#include <QString>
#include <QVariant>
#include <QMap> class Configure
{
public:
Configure();
void init();
void setPortName(QString portName);
QString getPortName(); void setBaudRate(QString baudRate);
QString getBaudRate(); void setResponseTime(int responseTime);
int getResponseTime(); void setNumberOfRetries(int numBerOfRetries);
int getNumberOfRetries();
private:
QString configureFilePath_; }; #endif // CONFIGURE_H
#include "configure.h"
#include <QSettings>
#include <QDebug>
#include <QCoreApplication> const QString configureFileName="config.conf";
Configure::Configure()
{
configureFilePath_=QCoreApplication::applicationDirPath()+"/"+configureFileName;
}
void Configure::init()
{
QSettings settings(configureFilePath_, QSettings::IniFormat); if(settings.value ("Modbus/PortName").toString ()=="")
{
settings.setValue("Modbus/PortName","ttyUSB0");
}
if(settings.value ("Modbus/BaudRate").toString ()=="")
{
settings.setValue("Modbus/BaudRate","");
}
if(settings.value ("Modbus/ResponseTime").toString ()=="")
{
settings.setValue("Modbus/ResponseTime",);
}
if(settings.value ("Modbus/NumberOfRetries").toString ()=="")
{
settings.setValue("Modbus/NumberOfRetries",);
} }
void Configure::setPortName(QString portName)
{
QSettings settings(configureFilePath_, QSettings::IniFormat);
settings.setValue("Modbus/PortName",portName);
} QString Configure::getPortName()
{
QSettings settings(configureFilePath_, QSettings::IniFormat);
return settings.value("Modbus/PortName").toString();
} void Configure::setBaudRate(QString baudRate)
{
QSettings settings(configureFilePath_, QSettings::IniFormat);
settings.setValue("Modbus/BaudRate",baudRate);
} QString Configure::getBaudRate()
{
QSettings settings(configureFilePath_, QSettings::IniFormat);
return settings.value("Modbus/BaudRate").toString();
} void Configure::setResponseTime(int responseTime)
{
QSettings settings(configureFilePath_, QSettings::IniFormat);
settings.setValue("Modbus/ResponseTime",responseTime);
} int Configure::getResponseTime()
{
QSettings settings(configureFilePath_, QSettings::IniFormat);
return settings.value("Modbus/ResponseTime").toInt ();
} void Configure::setNumberOfRetries(int numBerOfRetries)
{
QSettings settings(configureFilePath_, QSettings::IniFormat);
settings.setValue("Modbus/NumberOfRetries",numBerOfRetries);
} int Configure::getNumberOfRetries()
{
QSettings settings(configureFilePath_, QSettings::IniFormat);
return settings.value("Modbus/NumberOfRetries").toInt ();
}
[Modbus]
BaudRate=
NumberOfRetries=
PortName=ttyUSB0
ResponseTime=

Qt ini配置文件读写的更多相关文章

  1. 【转载】C++针对ini配置文件读写大全

    http://blog.csdn.net/hgy413/article/details/6666374# ini文件(Initialization file),这种类型的文件中通常存放的是一个程序的初 ...

  2. vc ini配置文件读写

    ini文件(即Initialization file),这种类型的文件中通常存放的是一个程序的初始化信息.ini文件由若干个节(Section)组成,每个Section由若干键(Key)组成,每个Ke ...

  3. C# INI配置文件读写类

    ini是一种很古老的配置文件,C#操作ini文件借助windows底层ini操作函数,使用起来很方便: public class IniHelper { [DllImport("kernel ...

  4. C++[类设计] ini配置文件读写类config

      //in Config.h #pragma once #include <windows.h> #include <shlwapi.h> #pragma comment(l ...

  5. 纯C#的ini格式配置文件读写

    虽然C#里都是添加app.config 并且访问也很方便 ,有时候还是不习惯用他.那么我们来做个仿C++下的那种ini配置文件读写吧,其他人写的都是调用非托管kernel32.dll.我也用过 但是感 ...

  6. c#读写ini配置文件示例

    虽然c#里都是添加app.config 并且访问也很方便 ,有时候还是不习惯用他.那么我们来做个仿C++下的那种ini配置文件读写吧     其他人写的都是调用非托管kernel32.dll.我也用过 ...

  7. C#操作读写INI配置文件

    一个完整的INI文件格式由节(section).键(key).值(value)组成.示例如:[section]key1=value1key2=value2; 备注:value的值不要太长,理论上最多不 ...

  8. Qt读写三种文件,QSettings读ini配置文件,QJsonDocument读JSON文件,QDomDocument读xml文件

    第一种INI配置文件 .ini 文件是Initialization File的缩写,即初始化文件. 除了windows现在很多其他操作系统下面的应用软件也有.ini文件,用来配置应用软件以实现不同用户 ...

  9. QT 读写.ini配置文件

    当需要存放的数据量较少时合适使用.ini配置文件. #include <QCoreApplication> #include <QSettings> void SystemSe ...

随机推荐

  1. 柱状堆积图Echarts

    Map<String,Object> map = new HashMap<String, Object>(); //图例的千人.双百 HashMap<String, St ...

  2. gradle providedCompile 与compile区别

    Gradle compile: 如果你的jar包/依赖代码 在编译的时候需要依赖,在运行的时候也需要,那么就用compile例如 : compile 'org.springframework.boot ...

  3. 分布式缓存之 memcache 实现分布式缓存

    最近想搞点分布式,但是不知道整点什么,来点简单的吧. 今天讲下memcache的分布式缓存 首先下载memcache的服务器端 百度下可以找到 然后执行安装和开启(关闭服务器)命令(还有其他的命令 可 ...

  4. CF460B Little Dima and Equation (水题?

    Codeforces Round #262 (Div. 2) B B - Little Dima and Equation B. Little Dima and Equation time limit ...

  5. javascript数组的知识点讲解

    javascript数组的知识点讲解 阅读目录 数组的基本方法如下 concat() join() pop() push() reverse() shift() sort() splice() toS ...

  6. [译]angularjs directive design made easy

    原文: http://seanhess.github.io/2013/10/14/angularjs-directive-design.html AngularJS directives很酷 Angu ...

  7. INSTALL_FAILED_INSUFFICIENT_STORAGE(转发)

    [2012-06-19 14:06:47 - Icontacts] Installation error: INSTALL_FAILED_INSUFFICIENT_STORAGE[2012-06-19 ...

  8. 一张图告诉你,只会jQuery还不够!

    会了jquery语法,会了jquery函数,你就真的会了jquery吗,来看这张图!是超实用的jquery代码段一书的导览!熊孩子们,赶紧学习去吧! 对于码农来说,代码就是生产力,你每天能码多少行并不 ...

  9. PHP文件的上传与下载

    文件上传: 1.单个与多个文件上传 2.上传表单的属性设置 3.PHP配置文件中相关文件上传的设置 4.PHP处理上传的文件数据 php.ini配置: file_uploads = on; 默认on ...

  10. Hdu.1325.Is It A Tree?(并查集)

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...