views:

101

answers:

4

Hello,

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 :/

+1  A: 

Presumably you relly mean;

* (this->variable1Pointer) = true;

which you can shorten to:

* variable1Pointer = true;

although the design of this class seems wrong to me - classes should normally not be modifying things that are not themselves members of the class.

anon
+1  A: 

It is not very clear what exactly you are trying to do but just looking at the syntax error, you need some thing like this

*(this->variable1Pointer) = true
lamirap
+1  A: 

Some suggestions:

  • Use references instead of pointers, they are usually very similar to pointers in use, but a little bit safer and easier. Just write bool & variable1Ref and then you can use it as a variable.
  • Use initiliazer list in the constructor
  • mixing data and its presentation in one class OptForm is a bad idea. It can be fine for small applications, but things easily get messed up when the application grows. Use a data storage class (Model) and a class to show it (View).
IanH
You *must* use an initializer list if you're going to use references anyway.
strager
Um, references as class members are fraught with problems - this looks like bad advice to me.
anon
EdgeLuxe
@strager: Yes, you are right. Same as with const pointers.@Neil: Whats the problem with them? (beside the same problems as pointers to externally created objects have?)
IanH
@EdgeLuxe: Looks good, but you need to use the parameter names (expertR and verboseR) as arguments in the initialiser list.
IanH
A: 

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():

OptDialog w (this->variable1,this->variable2, this); //must inherit from QDialog instead of QWidget
if ( w.exec() == QDialog::Accepted ) { // blocks until the user closes the dialog
    //process input
}

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:

OptDialog w (this->variable1,this->variable2, this); 
w.setSomeBoolOption( settings.someBoolOption() );
w.setAnotherBoolOption( settings.anotherBoolOption() );
if ( w.exec() == QDialog::Accepted ) { //user accepted, apply changes to settings:
    settings.setSomeBoolOption( w.someBoolOption() );
    settings.setAnotherBoolOption( w.anotherBoolOption() );
}
Frank