Originally Posted by
amleto
one more question, the MainWindow initially keeps the default setting value which can pass through to the settings mainwindow when it is called. it can also be stored in the settings window though. My question is that whenever the settings window closes, new updated settings gets lost!
What do you think the ConfigSource is for?
I want you to help me clarify the following problem please.
1. I do not know if this way is but, but I added a reference to ConfigSource in the Settings class constructor. So, before the Main Window get destructed, I can save the src_ values permanently to a file. I hope this is not redundant, or maybe we don't need it as the ConfigSource and updater classes exist. ?
2. As we used a reference to refer to src_, the program gives unexpected error when I call it's cancel and update methods to update it.
This is inside Main Window constructor, before I call the Settings Window
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
ConfigSource cfg;
settings = new SettingsWindow(cfg,this);
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ConfigSource cfg;
settings = new SettingsWindow(cfg,this);
To copy to clipboard, switch view to plain text mode
Settings and Config classes
#ifndef SETTINGSWINDOW_H
#define SETTINGSWINDOW_H
#include <QMainWindow>
#include <QModelIndex>
#include <QSignalMapper>
#include <QRadioButton>
#include <QSpinBox>
#include <QTimer>
#include "cameracommands.h"
struct Config
{
/* General Options */
};
class ConfigSource
{
public:
ConfigSource() {
config_.general_aperture_control = "Reset";
config_.general_automatic_exposure = "Full Auto";
config_.general_back_light = "OFF";
}
Config config() const {return config_;}
void setConfig(const Config& cfg) {config_ = cfg;}
private:
Config config_;
};
class ConfigUpdater
: public QObject{
Q_OBJECT
public:
ConfigUpdater(ConfigSource& src) : src_(src) {}
public slots:
void apply () {src_.setConfig(tempConfig_);}
void cancel() {tempConfig_ = src_.config();}
public:
void updateGeneralBackLight
(QString s
) {tempConfig_.
general_back_light = s; qDebug
() <<
"BackLight updated :)";
} void updateGeneralKeyLock
(QString s
) {tempConfig_.
general_key_lock = s; qDebug
() <<
"KeyLock updated :)";
}
Config tempConfig_;
ConfigSource& src_;
};
//----------------------------
namespace Ui {
class SettingsWindow;
}
{
Q_OBJECT
public:
explicit SettingsWindow
(ConfigSource
&src,
QWidget *parent
= 0);
~SettingsWindow();
private slots:
void on_pushButton_5_clicked();
void on_pushButton_3_clicked();
void on_sendToMainButton_clicked();
private:
void print_config(Config cfg);
Ui::SettingsWindow *ui;
ConfigUpdater *config_updater;
};
#endif // SETTINGSWINDOW_H
#ifndef SETTINGSWINDOW_H
#define SETTINGSWINDOW_H
#include <QMainWindow>
#include <QModelIndex>
#include <QSignalMapper>
#include <QRadioButton>
#include <QSpinBox>
#include <QTimer>
#include "cameracommands.h"
struct Config
{
/* General Options */
QString general_key_lock;
QString general_back_light;
};
class ConfigSource
{
public:
ConfigSource() {
config_.general_aperture_control = "Reset";
config_.general_automatic_exposure = "Full Auto";
config_.general_back_light = "OFF";
}
Config config() const {return config_;}
void setConfig(const Config& cfg) {config_ = cfg;}
private:
Config config_;
};
class ConfigUpdater : public QObject
{
Q_OBJECT
public:
ConfigUpdater(ConfigSource& src) : src_(src) {}
public slots:
void apply () {src_.setConfig(tempConfig_);}
void cancel() {tempConfig_ = src_.config();}
public:
void updateGeneralBackLight(QString s) {tempConfig_.general_back_light = s; qDebug() << "BackLight updated :)";}
void updateGeneralKeyLock(QString s) {tempConfig_.general_key_lock = s; qDebug() << "KeyLock updated :)";}
Config tempConfig_;
ConfigSource& src_;
};
//----------------------------
namespace Ui {
class SettingsWindow;
}
class SettingsWindow : public QMainWindow
{
Q_OBJECT
public:
explicit SettingsWindow(ConfigSource &src, QWidget *parent = 0);
~SettingsWindow();
private slots:
void on_pushButton_5_clicked();
void on_pushButton_3_clicked();
void on_sendToMainButton_clicked();
private:
void print_config(Config cfg);
Ui::SettingsWindow *ui;
ConfigUpdater *config_updater;
};
#endif // SETTINGSWINDOW_H
To copy to clipboard, switch view to plain text mode
SettingsWindow
::SettingsWindow(ConfigSource
&src,
QWidget *parent
) : ui(new Ui::SettingsWindow)
{
/* initializations */
ui->setupUi(this);
config_updater = new ConfigUpdater(src);
SettingsWindow::SettingsWindow(ConfigSource &src, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::SettingsWindow)
{
/* initializations */
ui->setupUi(this);
config_updater = new ConfigUpdater(src);
To copy to clipboard, switch view to plain text mode
As I mentioned above, the program terminates when I attempt to call he Cancel and Update method!
config_updater->cancel();
Bookmarks