【问题标题】:How to store in Qt Settings?如何存储在 Qt 设置中?
【发布时间】:2012-03-09 09:00:59
【问题描述】:

我目前正在创建一个带有“不再显示”复选框的对话框。单击复选框并关闭对话框(按下确定按钮)时,应用程序将在QSettings 中存储该对话框之前已打开的信息。

对Qt设置不熟悉,看API也不知道用哪个函数。

谁能指出我正确的方向?谢谢!

顺便说一句,我确实尝试了QErrorMessage,但消息框不断弹出,所以我放弃了。

void MessageBox::on_checkBox_stateChanged(int arg1)
{
    if(ui->checkBox->stateChanged(arg1) && ui->pushButton->clicked(true))
    //I believe this is right.
    {
       writeSettings();
    }
}

void MessageBox::writeSettings()
{
    QSettings settings;
//...help; Question: Should I write in main.cpp or in the .h?
}

void MessageBox::readSettings()
{
//...help
}

【问题讨论】:

  • 看看这个问题:stackoverflow.com/q/3597900/2796,你应该找到你正在寻找的关于QSettings使用的答案。
  • @Jérôme 谢谢! :) 我会保留那个标签作为参考!

标签: qt


【解决方案1】:

要以这种形式使用 QSettings 构造函数,您必须为您的应用程序设置组织和应用程序名称,如果您在那里创建它,可能在 main.cpp 中:

QApplication a(argc, argv);
a.setOrganizationName("MySoft");
a.setApplicationName("Star Runner");

然后在您的 writeSettings() 中执行:

QSettings settings;
settings.setValue("showErrorMessages", ui->checkBox->isChecked());

在 readSettings() 中

QSettings settings;
bool showErrorMessages = settings.value("showErrorMessages", true).toBool()

这一切都在docs 中,IMO 解释得很清楚。

【讨论】:

  • 真的,哇,我想知道设置值的目的是什么以及如何使用它。现在对一个月前开始使用 qt 的人提出一个简短的问题,哈哈,你有 a.setOrganizationName()a.setAplicationName()。这会覆盖当前名称,即MainWidow::setWindowTitle("example")
猜你喜欢
  • 2015-11-04
  • 2016-09-17
  • 1970-01-01
  • 2019-07-07
  • 1970-01-01
  • 2017-07-11
  • 1970-01-01
  • 1970-01-01
  • 2021-06-28
相关资源
最近更新 更多