Results 1 to 5 of 5

Thread: How to call a static method from another thread without having to use signal and slot

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How to call a static method from another thread without having to use signal and slot

    Hello!

    I have the following situation: I created a new class MessageFrame that should do the work of QMessageBox for an embedded solution, having the ::question, ::information, etc. static methods to be called from when I want. The main difference, though, from the QMessageBox class is that, instead of directly creating the static dialog, MessageFrame is implemented with the Singleton pattern (one static instance of the class is created and the static methods mentioned earlier all call to that unique instance).

    Everything was fine till I needed to call those static methods from within a different thread then the main thread. When I did that, Qt began to send lots of errors and no MessageFrame at all and I remembered that there is such a problem of calling a show() method from an object from another thread.

    The question is: how may I solve this problem? I know of at least one solution, but that I want to avoid: each time I want to call one of those static methods from this different thread, I emit a signal that is somewhere else connected to those static methods, now converted to slots. This would work as a signal and slot connection between different threads and, therefore, MessageFrame's show() would be called from inside the proper thread. But as I sad, I want to avoid this method.

    So, is there another way of doing this?



    Thanks,

    Momergil
    May the Lord be with you. Always.

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to call a static method from another thread without having to use signal and

    Hi,

    As Qt doc says, only the main thread can touch the GUI, so you have to use SIGNAL/SLOT mechanism.

    What you would to achieve?
    Òscar Llarch i Galán

  3. The following user says thank you to ^NyAw^ for this useful post:

    Momergil (26th June 2014)

  4. #3
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to call a static method from another thread without having to use signal and

    Quote Originally Posted by ^NyAw^ View Post
    What you would to achieve?
    The idea is to use MessageFrame as close to QMessageBox as possible, that is, when I want to use it, I just call the static methods directly; no such kind of "previous configuration needed" wanted :/
    May the Lord be with you. Always.

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to call a static method from another thread without having to use signal and

    You can use QMetaObject::invokeMethod() to call slots like if they were called through a signal/slot connection.
    You can obviously hide that behind a static method that has access to the receiver object pointer.

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    Momergil (26th June 2014)

  7. #5
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to call a static method from another thread without having to use signal and

    Quote Originally Posted by anda_skoa View Post
    You can use QMetaObject::invokeMethod()
    Thanks, anda_skoa! It worked perfectly


    Momergil
    May the Lord be with you. Always.

Similar Threads

  1. How to call a static method of java from c++ side?
    By stereoMatching in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 31st December 2013, 15:54
  2. Call method of Object in different Thread
    By Qiieha in forum Qt Programming
    Replies: 3
    Last Post: 26th July 2012, 13:14
  3. Call Non-static method on static method
    By METEOR7 in forum Qt Programming
    Replies: 7
    Last Post: 3rd May 2012, 21:26
  4. None-GUI thread call widget method cause error
    By luochen601 in forum Qt Programming
    Replies: 3
    Last Post: 28th July 2010, 05:39
  5. Signal/slot or direct method call within a class
    By mike_the_tv in forum Newbie
    Replies: 6
    Last Post: 11th March 2010, 18:49

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.