Results 1 to 3 of 3

Thread: Invoking a slot from a static method in time

  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 Invoking a slot from a static method in time

    Hello!

    Some time ago I open this thread to learn how could I call a static method from another thread. Back then I learned about the QMetaObject::invokeMethod which has since served me well.

    Now the situation has slightly changed: I call my static method from whatever thread I want and, in that static method, I use QMetaObject::invokeMethod to a call a Singleton class's method:

    Qt Code:
    1. void MessageFrame::critical(const message &text)
    2. {
    3. if (Q_UNLIKELY(!QMetaObject::invokeMethod(&instance(),"showMessage",Q_ARG(MessageFrame::Icon,MessageFrame::Critical),Q_ARG(MessageFrame::message,text))))
    4. mDebugS("Failed invokeMethod in MessageFrame::critical");
    5. }
    To copy to clipboard, switch view to plain text mode 

    Everything works fine, but I want to add a new feature: time control. I want to not simply call the static method, but tell it that, after some time, another method of the same Singleton class needs to be called:

    Qt Code:
    1. void MessageFrame::critical(const message &text, const qint32 time)
    2. {
    3. if (Q_UNLIKELY(!QMetaObject::invokeMethod(&instance(),"showMessage",Q_ARG(MessageFrame::Icon,MessageFrame::Critical),Q_ARG(MessageFrame::message,text))))
    4. mDebugS("Failed invokeMethod in MessageFrame::critical");
    5.  
    6. if (time > 0)
    7. QTimer::singleShot(time,&instance(),SLOT(nextMessage()));
    8. }
    To copy to clipboard, switch view to plain text mode 

    Something like that. The problem is: how can I do this? I have no idea apart from knowing that the above QTimer::singleShot() call don't work.


    Momergil
    May the Lord be with you. Always.

  2. #2
    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: Invoking a slot from a static method in time

    If that doesn't work then the thread probably doesn't run an event loop and therefore can't process the timer event.

    Since the cross-thread calling requires the owner thread of instance() to run one, you could add a method there that does that and call it via invokeMethod as usual.

    Cheers,
    _

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

    Momergil (28th August 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: Invoking a slot from a static method in time

    Thanks for the reply, anda_skoa, but doing some extra research I found out that actually the QTimer call was being made; it was the implementation of nextMessage() that was problematic and wasn't doing what I wanted

    Everything working fine now.

    Guess that should teach me not to come in such a hurry to a forum :x

    Momergil
    May the Lord be with you. Always.

Similar Threads

  1. Replies: 4
    Last Post: 26th June 2014, 18:27
  2. Call Non-static method on static method
    By METEOR7 in forum Qt Programming
    Replies: 7
    Last Post: 3rd May 2012, 21:26
  3. How to emit signal from static method?
    By kusumat in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2011, 09:17
  4. Invoking a slot in another thread
    By dorik in forum Qt Programming
    Replies: 5
    Last Post: 11th July 2011, 18:42
  5. Static field and public method
    By probine in forum General Programming
    Replies: 1
    Last Post: 5th March 2006, 11:02

Tags for this Thread

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.