Qt / C ++写/读键值对文件 [英] Qt/C++ Write/Read Key-Value Pair File

查看:672
本文介绍了Qt / C ++写/读键值对文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Qt / C ++构件的Mac应用程序。我有一系列复选框,单选按钮和用于存储设置的文本字段。我想将它们保存到一个键值对文件,当我点击一个更新按钮,然后当应用程序加载时,阅读这个文件来设置这些小部件回到适当的状态。



有没有一个例子说明这是怎么做到的? 解决方案

以下是我在Mac上使用的。但是,如果修改设置对象的 setPath(),则可以使其在其他平台上通过<一个href =http://doc.qt.io/qt-5/qsettings.html#QSettings =nofollow>文档。

< mainwindow.h ,我不得不添加这些条目:

  void readSettings(); 
void writeSettings();

在Private Slots下。

mainwindow.cpp 中,我添加了这些示例函数:

  void MainWindow :: readSettings()
{
QSettings设置;
settings.setPath(settings.IniFormat,settings.SystemScope,/ etc / xdg);
settings.beginGroup(General);
ui-> checkBox1-> setChecked(settings.value(checkBox1,false).toBool());
settings.endGroup();
}

void MainWindow :: writeSettings()
{
QSettings设置;
settings.setPath(settings.IniFormat,settings.SystemScope,/ etc / xdg);
settings.beginGroup(General);
settings.setValue(checkBox1,ui-> checkBox1-> isChecked());
settings.endGroup();





$ b您应该阅读关于范围和 setPath( ) QSettings页面获取更多信息。
$ b 然后,在 MainWindow :: MainWindow()函数中,我这样做了:

  QCoreApplication :: setOrganizationName(Example Company); 
QCoreApplication :: setOrganizationDomain(example.com);
QCoreApplication :: setApplicationName(Example Product);

ui-> setupUi(this);
this-> readSettings();

请注意,您只能使用 readSettings()在设置了 ui 变量之后。通过像我一样使用 QCoreApplication 类,我不必一直重新设置我正在使用的设置。


$然后,我把 MainWindow 并添加了一个 checkBox1 和一个 updateButton 就可以了,并设置updateButton clicked()槽代码为:

 这 - > writeSettings(); 

现在当窗口加载时,绘制UI之后,读取设置以设置复选框值到您点击更新按钮时设置的任何内容。


I have a Qt/C++ widget based application for the Mac. I have a series of checkboxes, radio buttons, and text fields for storing settings. I want to save them to a key value pair file when I click an Update button, and then when the application loads, read this file to set those widgets back to the proper states.

Is there an example for how this is done somewhere?

解决方案

The following is what I used on a Mac. However, if you modify the setPath() of your settings object, you can make it work on other platforms via the documentation.

In mainwindow.h, I had to add these entries:

void readSettings();
void writeSettings();

under Private Slots.

Then, in mainwindow.cpp, I added these example functions:

void MainWindow::readSettings()
{
    QSettings settings;
    settings.setPath(settings.IniFormat,settings.SystemScope,"/etc/xdg");
    settings.beginGroup("General");
    ui->checkBox1->setChecked(settings.value("checkBox1",false).toBool());
    settings.endGroup();
}

void MainWindow::writeSettings()
{
    QSettings settings;
    settings.setPath(settings.IniFormat,settings.SystemScope,"/etc/xdg");
    settings.beginGroup("General");
    settings.setValue("checkBox1",ui->checkBox1->isChecked());
    settings.endGroup();
}

You should read about "scope" and setPath() on the QSettings page for more information.

Then, in MainWindow::MainWindow() function, I had it like so:

QCoreApplication::setOrganizationName("Example Company");
QCoreApplication::setOrganizationDomain("example.com");
QCoreApplication::setApplicationName("Example Product");

ui->setupUi(this);
this->readSettings();

Note that you can only readSettings() after you have set the ui variable. And by using the QCoreApplication class like I did, I don't have to keep redeclaring what settings I'm using all the time.

Then, I took the MainWindow and added a checkBox1 and an updateButton on it, and set the updateButton clicked() slot code to this:

this->writeSettings();

Now when the window loads, after the UI is drawn, it reads the settings to set the checkbox value to whatever you set when you clicked the update button.

这篇关于Qt / C ++写/读键值对文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆