Results 1 to 1 of 1

Thread: How to send custom events from QThread:: run () to application.

  1. #1
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to send custom events from QThread:: run () to application.

    Hello.

    I have a need to send a custom event from a thread into the application, but I have nothing.

    I do so:

    1. My custom event:
    Qt Code:
    1. class CustomEvent : public QEvent
    2. {
    3. public:
    4. CustomEvent ()
    5. : QEvent((QEvent::Type)(QEvent::User + 1)) {}
    6. ...
    7. ...
    8. }
    To copy to clipboard, switch view to plain text mode 

    2. My implementation of a class QThread:
    Qt Code:
    1. void MyThread::run()
    2. {
    3. while (!stopped) {
    4. //Processing.
    5. ...
    6. ...
    7.  
    8. //For some conditions, generates an event.
    9.  
    10. CustomEvent *e = new CustomEvent ();
    11. QCoreApplication::postEvent(qApp, e);
    12.  
    13. //Processing.
    14. ...
    15. ...
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    3. My main (and only) widget application:
    Qt Code:
    1. void TestWidget::customEvent(QEvent *e)
    2. {
    3. if (e->type() == (QEvent::User + 1)) {
    4.  
    5. qDebug() << "TestWidget::customEvent() caught";
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    But I have a message "caught" is displayed. What am I doing wrong?

    PS: I tried to override the same method "eventFilter()", as well as install "installEventFilter(qApp)" but nothing happens. Help please.


    Added after 21 minutes:


    Oops, sorry problem solved.

    Instead
    Qt Code:
    1. myWidget->installEventFilter(qApp)
    To copy to clipboard, switch view to plain text mode 
    need
    Qt Code:
    1. qApp->installEventFilter(myWidget)
    To copy to clipboard, switch view to plain text mode 

    Last edited by kuzulis; 26th April 2011 at 11:02.

Similar Threads

  1. is there any api can send signal/message to other application
    By guchangyuan in forum Qt Programming
    Replies: 7
    Last Post: 27th July 2009, 09:04
  2. send an email from my Qt application
    By omega36 in forum Qt Programming
    Replies: 14
    Last Post: 22nd December 2008, 17:06
  3. Replies: 2
    Last Post: 1st August 2008, 16:58
  4. Posting custom events to a subclass of QThread
    By jpn in forum Qt Programming
    Replies: 3
    Last Post: 4th July 2006, 15:49
  5. Replies: 3
    Last Post: 6th February 2006, 17:41

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.