I have the following layout:
MainWindow <--------- Settings
\
\
V
PerformOps
MainWindow takes in variables passed from Settings, and passes them on to PerformOps.
If the Settings class is not used, MainWindow passed on defaults to PerformOps.
Currently I do:
class Settings{
public:
Settings(*parent);
var1, var2, var3
.
.
void exec();
}
class PerformOps{
public:
PerformOps();
var1, var2, var3;
.
.
void start();
}
class MainWindow{
private:
Settings *set; //share pointers over all methods
PerformOps *op;
bool settings_not_clicked = false;
void saveChanges()
{
QSettings settings("my","app");
settings.setValue("value1", op->var1);
settings.setValue("value2", op->var2);
settings.setValue("value3", op->var3);
}
void loadChanges()
{
QSettings settings("my","app");
op->var1 = settings.value("value1");
op->var2 = settings.value("value2");
op->var3 = settings.value("value3");
}
void closeEvent(QCloseEvent *event)
{
event->ignore();
saveChanges();
event->accept();
}
void takeDataAndDoStuff() //This is always called
{
op = new PerformOps;
if(settings_not_clicked) {
loadChanges()
}
else {
op->var1 = 33;
op->var2 = "Geronimo!";
op->var3 = true;
}
op->start();
}
void getStuff_maybe_if_clicked() //This might not be always called
{
Settings *set = new Settings(this);
set->exec() //Pause parent, run Settings
settings_not_clicked = false;
}
Question: Is there a cleaner way to share data across classes without resorting to the dirty method of : op->var1 = set->var1;, taking into account that the set pointer might not always be initialised?
Well, the approach itself is not that bad, however there are some things you can improve.
First of all, if I understood you correctly, you want to pass the settings if they exist, and pass the default values if they don’t. In this case you can utilize the constructor:
Now, if you call the constructor as
PerformOps(), the default values will be set. If you call it and feed it some of the values, it will use them:of course, if you don’t want to do it via the cunstructor, you could just make a function and call it like “setData()” and use the same technique with default function parameters.
Now, as for the pointers. It is a good idea to always initialize pointers with
NULL, ornullptrif you have c++0x. Also, when you delete the memory, assign the pointer toNULLornullptragain. This way you will be able to always see if the pointer is valid by a simple check.UPD
I would suggest you to get rid of your
Settingsclass, and just use QSettings directly. You will not need to mess with the pointers, and reading/writing to QSettings is very fast.Now, also don’t forget that you can use QSettings from the heap:
If you want each settings set to have a “parent”, you could just derive your
Settingsclass fromQSettings, and just add oneparentfield into it. This will keep all the functionality ofQSettings, which is very convenient.Actually, your approach is fine too, all you need to do is just to check if the pointers are valid.