PDA

View Full Version : Closing dialog by QMetaObject::invokeMethod - safe?



Blood9999
16th August 2012, 10:47
In my program i have many dialogs, which actions depends on if data from database are downloaded correctly. If some error occurs i don't want to event show dialog. I tried something like this:


/* dialog constructor*/
if(error)
this->reject();

but it doesn't work. Then i found something like this:


/* dialog constructor*/
if(error)
QMetaObject::invokeMethod(this, "close", Qt::QueuedConnection);

I read something about this method, but actually i'm still not sure if it's safe for my program. How it works? After invoking this method, my class destructor will be called to release allocated memory? If not, how can i achieve that?

spirit
16th August 2012, 11:01
How it works? After invoking this method, my class destructor will be called to release allocated memory? If not, how can i achieve that?
Invocation will be posted in an even loop's events queue and it will be call when it's being met in the queue, eg not immediately.

If you set Qt::WA_DeleteOnClose for your dialog then yes the dtor will be called.

Blood9999
16th August 2012, 11:10
Oh, but most important - why my first version don't work? WHy i can't invoke reject() or accept() in QDialog constructor?

spirit
16th August 2012, 11:11
In ctor (it's really bad idea)? Because it's not shown yet I guess ;)

yeye_olive
16th August 2012, 11:21
but it doesn't work.

Oh, but most important - why my first version don't work?
I believe it "doesn't work" because you do something wrong. If you want a more detailed answer, please provide a more detailed description of what "doesn't work".

In any case, why do you create the dialog before having secured the data from the database? I would have done it the other way around.

Blood9999
16th August 2012, 11:32
I believe it "doesn't work" because you do something wrong. If you want a more detailed answer, please provide a more detailed description of what "doesn't work".

Dialog doesn't close, just like this function is not invoked.


In any case, why do you create the dialog before having secured the data from the database? I would have done it the other way around.

It's because i would have to pass all data by constructor arguments, which is a lot.

spirit
16th August 2012, 11:35
It's because i would have to pass all data by constructor arguments, which is a lot.
Well, you can validate data before showing the dialog, that allows you to decide to show or not to show the dialog.

yeye_olive
16th August 2012, 12:48
Dialog doesn't close, just like this function is not invoked.
The dialog will only close when control returns to the event loop. We need to see more code to understand what goes wrong.

It's because i would have to pass all data by constructor arguments, which is a lot.
You can wrap all this data in a nice structure that you pass to the dialog by reference.