QT application programming: application configuration save and restore

1. Environmental introduction

Operating system introduction: win10 64 bit

QT version:  5.12.6

2. Function introduction

 When the application is running, users often make some personalized settings for the application; after closing the application, the last time the application is opened will restore the last settings. In the program, you can save these settings to a local file for storage, and then load them when you open the program next time.

The built-in QDataStream class of QT provides a method for serializing data streams. The data stream is a binary stream of encoded information, which has nothing to do with the host operating system, CPU or byte order 100%. QT's built-in QTextStream and QDataStream are both data stream-oriented. The functions that QTextStream can accomplish can be accomplished by QDataStream, but their focus is different. QTextStream is for text stream processing. QTextStream stores ordinary text data for reading. The main limitation of QTextStream is that it supports fewer input data types, such as: pixel map QPixmap, color QColor, these types of data cannot save. Binary data is used in many cases in actual software, such as saving the current software configuration information, status information, network data packet transmission, etc. The processing of these binary data requires the use of QDataStream.

QDataStream can decompose all C++ basic types, including types encapsulated by QT itself, into basic types in order for encapsulation.


Three, source code

     3.1 Example of storing data

qint32 lcd_image_w=1920;
qint32 lcd_image_h=1080;
qint32 image_val=80;
quint16 server_port=8888;  //服务器端口号
//配置文件名称
#define ConfigFile "cofnig.ini"


//从UI界面获取用户的个性化配置参数
lcd_image_w=ui->spinBox_w->value();
lcd_image_h=ui->spinBox_h->value();
image_val=ui->spinBox_image_val->value();
server_port=ui->spinBox_port->value();

/*保存数据到文件,方便下次加载*/
QString text;
text=QCoreApplication::applicationDirPath()+"/"+ConfigFile;
QFile filesrc(text);
filesrc.open(QIODevice::WriteOnly);
QDataStream out(&filesrc);
out << lcd_image_w;  //序列化写---宽
out << lcd_image_h;  //序列化写---高
out << image_val;  //序列化写---质量
out << server_port; //服务器端口号
filesrc.flush();
filesrc.close();

3.2 Example of reading data

qint32 lcd_image_w=1920;
qint32 lcd_image_h=1080;
qint32 image_val=80;
quint16 server_port=8888;  //服务器端口号
//配置文件名称
#define ConfigFile "cofnig.ini"


//读取配置文件
QString text;
text=QCoreApplication::applicationDirPath()+"/"+ConfigFile;

//判断文件是否存在
if(QFile::exists(text))
{
	QFile filenew(text);
	filenew.open(QIODevice::ReadOnly);
	QDataStream in(&filenew); // 从文件读取序列化数据
	in >> lcd_image_w >> lcd_image_h >> image_val>>server_port; //提取写入的数据
	filenew.close();

	//设置界面值
	ui->spinBox_w->setValue(lcd_image_w);
	ui->spinBox_h->setValue(lcd_image_h);
	ui->spinBox_image_val->setValue(image_val);
	ui->spinBox_port->setValue(server_port);
}

 

Guess you like

Origin blog.csdn.net/xiaolong1126626497/article/details/111624177