Results 1 to 8 of 8

Thread: Closing dialog by QMetaObject::invokeMethod - safe?

  1. #1
    Join Date
    Aug 2012
    Posts
    17
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Closing dialog by QMetaObject::invokeMethod - safe?

    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:

    Qt Code:
    1. /* dialog constructor*/
    2. if(error)
    3. this->reject();
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. /* dialog constructor*/
    2. if(error)
    3. QMetaObject::invokeMethod(this, "close", Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Closing dialog by QMetaObject::invokeMethod - safe?

    Quote Originally Posted by Blood9999 View Post
    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.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Aug 2012
    Posts
    17
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Closing dialog by QMetaObject::invokeMethod - safe?

    Oh, but most important - why my first version don't work? WHy i can't invoke reject() or accept() in QDialog constructor?

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Closing dialog by QMetaObject::invokeMethod - safe?

    In ctor (it's really bad idea)? Because it's not shown yet I guess
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Closing dialog by QMetaObject::invokeMethod - safe?

    Quote Originally Posted by Blood9999 View Post
    but it doesn't work.
    Quote Originally Posted by Blood9999 View Post
    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.

  6. #6
    Join Date
    Aug 2012
    Posts
    17
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Closing dialog by QMetaObject::invokeMethod - safe?

    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.

  7. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Closing dialog by QMetaObject::invokeMethod - safe?

    Quote Originally Posted by Blood9999 View Post
    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.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. #8
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Closing dialog by QMetaObject::invokeMethod - safe?

    Quote Originally Posted by Blood9999 View Post
    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.
    Quote Originally Posted by Blood9999 View Post
    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.

Similar Threads

  1. Problems with QMetaObject::invokeMethod
    By AlphaWolf in forum Qt Programming
    Replies: 3
    Last Post: 13th September 2012, 12:57
  2. Replies: 3
    Last Post: 20th January 2012, 16:23
  3. Replies: 0
    Last Post: 19th January 2012, 16:52
  4. How to use QMetaObject::invokeMethod?
    By niko in forum Qt Programming
    Replies: 3
    Last Post: 26th January 2008, 17:02
  5. Using QMetaObject::invokeMethod() doesn't work
    By bruccutler in forum Qt Programming
    Replies: 2
    Last Post: 21st February 2007, 23:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.