【问题标题】:Store settings of qt application using QSettings使用 QSettings 存储 qt 应用程序的设置
【发布时间】:2011-05-11 05:34:08
【问题描述】:

您好,我已经使用 qt 创建了一个应用程序,并且我设法使用 QSettings 保存了它的一些设置。

void DoneIt::writeSettings()
{
    QSettings settings("mycompany", "RightDoneIt");
    settings.beginGroup("DoneIt");
    settings.setValue("size", size());
    settings.setValue("pos", pos());
    settings.endGroup();
}

void DoneIt::readSettings()
{
    QSettings settings("mycompany", "RightDoneIt");
    settings.beginGroup("DoneIT");
    resize(settings.value("size", QSize(400, 400)).toSize());
    move(settings.value("pos", QPoint(200, 200)).toPoint());
    settings.endGroup();
}

这适用于窗口位置和大小。 我已经使用 qt 的设计器在我的应用程序中添加了一些小部件,我也想保存它们的状态。

我的一个小部件是一个单选按钮,我称之为 radioButtonbnw

如何保存其状态(选中或未选中)?

最佳做法是什么?

【问题讨论】:

    标签: c++ qt qt4 qsettings


    【解决方案1】:
    1. 将它们发送至QButtonGroup
    2. 使用QButtonGroup::setId 为该组中的每个单选按钮设置ID。
    3. 保存QButtonGroup::checkedId获取的选中按钮的ID。
    4. 恢复时使用QButtonGroup::button(id)获取该按钮的指针,调用QAbstractButton::setChecked

    顺便说一句:如果要保存主窗口工具栏和停靠小部件的当前状态,请使用QMainWindow::saveState

    【讨论】:

    • 拜托,你能更精确一点吗?这是我第一次与 QbuttonGroup 打交道。你能给我一些代码吗? 1000 感谢
    • 抱歉,我的项目很忙,没有现成的代码。
    • 没问题 :) 这是你的投票和积分 :) 祝你的项目好运
    【解决方案2】:

    这里是一个带有三个 QRadioButton 的示例小部件

    settingspane.h

    #ifndef SETTINGSPANE_H
    #define SETTINGSPANE_H
    
    #include <QWidget>
    #include <QSettings>
    
    class SettingsPane
          : public QWidget
    {
        Q_OBJECT
    
        QSettings *settings;
    
    public:
        explicit SettingsPane(QWidget *parent = nullptr);
    
    public slots:
        void handle_rb_buttonReleased(int id);
    };
    
    #endif // SETTINGSPANE_H
    

    settingspane.cpp

    #include "settingspane.h"
    
    #include <QVBoxLayout>
    #include <QHBoxLayout>
    #include <QRadioButton>
    #include <QButtonGroup>
    
    SettingsPane::SettingsPane(QWidget *parent)
       : QWidget(parent)
    {
        settings = new QSettings();
    
        auto mlayout = new QVBoxLayout();
        setLayout(mlayout);
    
        // create some buttons
        auto rb_frst = new QRadioButton("first");
        auto rb_scnd = new QRadioButton("second");
        auto rb_thrd = new QRadioButton("third");
        // container to organize groups of buttons (no visual)
        auto buttonGroup = new QButtonGroup();
        buttonGroup->addButton(rb_frst,0);
        buttonGroup->addButton(rb_scnd,1);
        buttonGroup->addButton(rb_thrd,2);
        // layout buttons for visual representation
        auto rb_layout = new QHBoxLayout();
        rb_layout->addWidget(rb_frst);
        rb_layout->addWidget(rb_scnd);
        rb_layout->addWidget(rb_thrd);
        mlayout->addLayout(rb_layout);
    
        // use Functor-Based Connection due to overloaded buttonReleased
        connect( buttonGroup,
                 SIGNAL(buttonReleased(int)),
                 this,
                 SLOT(handle_rb_buttonReleased(int)));
    
        // restore button from settings
        int id = settings->value("buttonGroup").toInt();
        buttonGroup->button(id)->setChecked(true);
    }
    
    void
    SettingsPane::handle_rb_buttonReleased(int id)
    {
        // save new status to the settings
        settings->setValue("buttonGroup", id);
    }
    

    在主窗口中使用它

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    class MainWindow
          : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = 0);
    };
    
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "settingspane.h"
    
    #include <QTabWidget>
    
    MainWindow::MainWindow(QWidget *parent)
       : QMainWindow(parent)
    {
        auto cwidget = new QTabWidget();
        setCentralWidget(cwidget);
    
        auto settingsPane = new SettingsPane();
        cwidget->addTab(settingsPane,"Settings");
    }
    

    main.cpp

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    

    结果:

    【讨论】:

      猜你喜欢
      • 2012-12-31
      • 1970-01-01
      • 1970-01-01
      • 2011-02-26
      • 2017-06-22
      • 1970-01-01
      • 2012-02-01
      • 2011-01-14
      • 2011-07-05
      相关资源
      最近更新 更多