So I have two forms in my project: MainWindow and Options Form (OptForm; QWidget);
Now, I create (simply dragging to a form) a QPushButton in MainWindow to open OptForm, and passing in variables, which OptForm can change.
void MainWindow::openOpt() //Slot; QPushButton calls(?) it
{
OptForm w (this->variable1,this->variable2, this);
w.show();
}
OptForm constructor is:
OptForm::OptForm(bool & variable1, bool & variable2, QWidget *parent)
: QWidget (parent)
{
variable1Pointer = &variable1;
variable2Pointer = &variable2;
ui.setupUi(this);
}
options.h has:
class OptForm : public QWidget
{
Q_OBJECT
public:
OptForm(bool & variable1, bool & variable2, QWidget *parent)
//Pointers for encrypt and verbose
bool * variable1Pointer;
bool * variable2Pointer;
public slots:
void change_variable1();
void change_variable2();
private:
Ui::OptForm ui;
};
Now, void change_variable1(); and void change_variable2(); changes booleans to true or false.
Now, in these functions I have a line
this->*variable1Pointer = true;
And I get compiler error:
'((OptForm*)this)->OptForm::variable1Pointer' cannot be used as a member pointer, since it is of type 'bool*'
How do I get things right? (FIXED, THANKS)
Other thing what I need, is to let MainWindow know, when OptForm has closed, to check if options have changed. So, where should I place this code? In openOpt, or create a slot, which will be executed(?), when OptForm closes? How can I send signal to MainWindow then?
Thanks in advance. (I guess I have messed things up quite much)
Ok, compiler error fixed, but now, when I press that button window appers and closes immediately :/
Right now your Widget is destroyed after being created (show() doesn’t block and your widget is created on the stack , that’s why you don’t see anything.
If you want to block until the window is closed and then process the result, you could use QDialog and call QDialog::exec():
Another option: create the dialog on the heap, connect the finished() signal to another slot and call open().
Also note that if you create widgets or dialogs on the heap, they are not deleted before their parent dialogs are deleted (never if you don’t set a parent), unless you delete them manually or call setAttribute( Qt::WA_DeleteOnClose ) on them.
And: don’t pass pointers around, use setters/getters and apply the changes if the user accepts the dialog. E.g., if operating on some “settings” object: