5

こんにちは私は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のデザイナーを使用してアプリケーションにウィジェットをいくつか追加しましたが、それらの状態も保存したいと思います。

私のウィジェットの1つはラジオボタンで、radioButtonbnwと呼んでいます。

状態(チェック済みまたはチェックなし)を保存するにはどうすればよいですか?

ベストプラクティスは何ですか?

4

2 に答える 2

7
  1. それらをに入れてくださいQButtonGroup
  2. QButtonGroup::setIdこのグループの各ラジオボタンのIDを設定するために使用します。
  3. チェックされたボタンのIDを保存しますQButtonGroup::checkedId
  4. QButtonGroup::button(id)復元時にを使用してこのボタンのポインタを取得し、を呼び出しますQAbstractButton::setChecked

ところで:メインウィンドウのツールバーとドックウィジェットの現在の状態を保存したい場合は、を使用してQMainWindow::saveStateください。

于 2010-11-17T04:48:34.023 に答える
0

ここに3つのウィジェットの例がありますQRadioButton

settingsspane.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

settingsspane.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();
}

結果:
ここに画像の説明を入力してください

于 2018-03-23T22:23:20.540 に答える