Results 1 to 2 of 2

Thread: event appears in thread without eventloop exec()

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question event appears in thread without eventloop exec()

    Help me, I do not understand it. My code works even I believe it shouldnt.

    I can receive an event in a thread even though the eventloop of the thread has not been started with exec.

    See the code appended. I used the example "calculatorform". When some special numbers are entered by user it sends a customevent
    to my thread. The thread displays then a messagebox. The thread is running, this->ii increments and it costs full power of 1of 4 processor cores.


    /***************** main.cpp ***************/
    Qt Code:
    1. #include <QApplication>
    2. #include <QEvent>
    3.  
    4. #include "calculatorform.h"
    5. #include "mythread.h"
    6.  
    7. QApplication * appptr;
    8. QObject * trhead;
    9. QEvent::Type my_customevent;
    10. int main(int argc, char *argv[])
    11. {
    12. QApplication app(argc, argv);
    13. CalculatorForm calculator;
    14. MyThread testit1;
    15. MyThread testit2;
    16. MyThread testit3;
    17. MyThread testit4;
    18. trhead = &testit1;
    19. appptr = &app;
    20. my_customevent = (QEvent::Type) QEvent::registerEventType ( -1 );
    21. testit1.start();
    22. //testit2.start();
    23. //testit3.start();
    24. // testit4.start();
    25. calculator.show();
    26. return app.exec();
    27. }
    To copy to clipboard, switch view to plain text mode 







    /***************** my_thread.cpp ***************/

    Qt Code:
    1. extern QEvent::Type my_customevent;
    2.  
    3.  
    4. void MyThread::run()
    5. {
    6. //exec();
    7. while(1) this->ii++;
    8. }
    9.  
    10. int myMessageBox(long value);
    11.  
    12. void MyThread::customEvent ( QEvent * event )
    13. {
    14. if (event->type() == my_customevent)
    15. {
    16. myMessageBox(this->ii);
    17. }
    18. else
    19. {
    20. QThread::customEvent(event);
    21. }
    22.  
    23. }
    24.  
    25. int myMessageBox(long value)
    26. {
    27. QMessageBox msgBox;
    28. QString result;
    29. QTextStream(&result) << "Resultcode = " << value;
    30. // result == "pi = 3.14"
    31.  
    32. msgBox.setText("This is a test message box");
    33. msgBox.setInformativeText(result);
    34. msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
    35. msgBox.setDefaultButton(QMessageBox::Save);
    36. int ret = msgBox.exec();
    37. return ret;
    38. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 28th January 2010 at 09:23. Reason: missing [code] tags

Similar Threads

  1. event or thread
    By damien in forum Qt Programming
    Replies: 6
    Last Post: 3rd October 2008, 17:43
  2. Thread eventLoop and run
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 8th May 2008, 19:36
  3. Thread+send event
    By Fastman in forum Qt Programming
    Replies: 20
    Last Post: 1st August 2007, 14:09
  4. Main Thread Event loop
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 20th March 2007, 12:10

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
  •  
Qt is a trademark of The Qt Company.