Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: thread problem

  1. #1
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default thread problem

    hi,

    i m showing a splash screen before my application starts. The splash screen has a loading icon(a gif image). After showing the splash screen, there is some heavy processing going on because of which the loading icon doesnt animate(it stays in one single frame until the application starts). Next thing i tried was to place qApp->processEvents() at various places inside the "heavy processing code". This makes the loading icon move a bit intermittently but still its not smooth. So next thing i tried was to start a thread at the beginning of the application and have a timer run there, on each timeout of which, qApp->processEvents() will be called. But with this code, loading icon doesnt move at all. Here is the code...

    Qt Code:
    1. #include "mythread.h"
    2. #include <QTimer>
    3. #include <QApplication>
    4. MyThread::MyThread(QObject *parent)
    5. : QThread(parent)
    6. {
    7.  
    8. }
    9.  
    10. MyThread::~MyThread()
    11. {
    12.  
    13. }
    14.  
    15. void MyThread::run()
    16. {
    17. QTimer *timer = new QTimer();
    18. connect(timer, SIGNAL(timeout()), this, SLOT(callProcessEvents()));
    19. timer->start(100);
    20. exec();
    21. }
    22.  
    23. void MyThread::callProcessEvents()
    24. {
    25. qApp->processEvents();
    26. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QLabel>
    2. #include <QMovie>
    3. #include <QSplashScreen>
    4. #include <QEventLoop>
    5. #include <QTimer>
    6. #include <QBitmap>
    7. #include "splashthread.h"
    8. #include "mythread.h"
    9.  
    10. int main(int argc, char *argv[])
    11. {
    12. QApplication a(argc, argv);
    13.  
    14.  
    15. MyThread *th = new MyThread(NULL);
    16. th->start();
    17. th->moveToThread(th);
    18.  
    19. QSplashScreen screen(QPixmap("C:\\Documents and Settings\\am002bh\\Desktop\\error-checkin.PNG"), Qt::WindowStaysOnTopHint);
    20.  
    21. QLabel label(&screen);
    22. QMovie *movie = new QMovie("C:\\Documents and Settings\\am002bh\\Desktop\\bigrotation2.gif");
    23.  
    24. movie->jumpToFrame(movie->frameCount());
    25. QPixmap WinMask = movie->currentPixmap();
    26. label.setMask(WinMask.mask());
    27.  
    28. label.setMovie(movie);
    29. movie->start();
    30.  
    31. screen.show();
    32. //sm heavy processing code
    33. th->quit();
    34. screen.close();
    To copy to clipboard, switch view to plain text mode 

    what am i missing..if i call qApp->processEvents from inside the "heavy processing code", the loading icon animates..if i call that from another thread, it doesnt move at all..i cant start a timer from inside the main function either(which is why i use another thread). i need this loading icon to animate at all times untail the appliction loads(heavy processing code execution is done). Any threading gurus, please let me know how to correctly do this.

    Thanks in Advance!
    Amulya

  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: thread problem

    Hi,

    I think that you have to use the thread to perform the heavy computation and let the main thread process events and update the GUI.
    So, let the main thread perform the animation and the other thread to perform the heavy computation. If the thread needs to send some message to the main thread use the SIGNAL SLOT mechanism.
    The thread that you have to code have not to have an event loop, it have to be something like this:

    Qt Code:
    1. void myClassThread::run()
    2. {
    3. while(!bFinish)
    4. {
    5. //Do some work
    6. }
    7. //It will exit "while" when "bFinish" is true, so then the "run" method will exit and so the thread will stop
    8. }
    To copy to clipboard, switch view to plain text mode 
    Òscar Llarch i Galán

  3. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: thread problem

    I would also add a small sleep to the worker thread's event loop, so that it doesn't gobble all the cpu power and gives the main thread a chance to perform normally.

    Qt Code:
    1. void myClassThread::run()
    2. {
    3. while(!bFinish)
    4. {
    5. //Do some work
    6. msleep(10);
    7. }
    8. //It will exit "while" when "bFinish" is true, so then the "run" method will exit and so the thread will stop
    9. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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: thread problem

    Hi,

    I would also add a small sleep to the worker thread's event loop, so that it doesn't gobble all the cpu power and gives the main thread a chance to perform normally.
    You don't need this.
    I have an application that contain 4 classes. Every class have 2 threads(heavy computation threads). The main thread responds perfectly to GUI events like mouse clicks, ...

    Note that the main thread is a thread, so it will fight to obtain CPU time as the others threads do the same.
    Think that maybe the main thread only needs a few "ticks" to perform its job.

    And note that this approach is not using an "eventLoop" as you said.
    Last edited by ^NyAw^; 18th February 2009 at 09:49.
    Òscar Llarch i Galán

  5. #5
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: thread problem

    hi guys,

    thanks for ur input, but i cant move the heavy processing code into a different thread cuz it involves many GUI operations is there ANY other way to do it like i did..what i dont understand is why qApp->processEvents() doesnt work out of a different thread..whereas it was working in the main thread.

  6. #6
    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: thread problem

    create propper signal/slot approach for separating gui and non-gui operation. try to revise your havy-compution algorithm.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #7
    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: thread problem

    Hi,

    If the heavy computation performs GUI, use SIGNAL SLOT mechanism to let the main thread do the GUI operations. It's not difficult.

    But if you really want to do it as you tell, why use a thread that starts a timer? Only start a timer on the main thread and try if the calls to "processEvents" work.
    Òscar Llarch i Galán

  8. #8
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: thread problem

    looks like a revising in the heavy-computation algorithm is in order....a connect in the main also doesnt work..its really strange....thanks guys for ur time!

  9. #9
    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: thread problem

    Hi,

    a connect in the main also doesnt work
    Is "yourClassThread" defining the "Q_OBJECT" macro?

    You can take a look at console output to see why it don't connect. Note that the connections have to be "Qt::QueuedConnection" or let it autodetect.
    Òscar Llarch i Galán

  10. #10
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: thread problem

    i meant i cant use connect(...) in main() because where would i put Q_OBJECT?? about Qt::QueuedConnection , yes i used that too..still didnt work..i m sure if i get to call processEvents() at a certain interval until the processing is over, i can animate the icon..but for sm reason, none of my attempts to call processEvents(), either from main() or other thread is working....

  11. #11
    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: thread problem

    create an object in main thread (in main.cpp) which will call qApp->processEvents by QTimer::timeOut.

    Qt Code:
    1. ....
    2. class MyProcessEventDispatcher: public QObject
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. MyProcessEventDispatcher(QObject *parent = 0)
    8. : QObject (parent)
    9. {
    10. m_timer.setInterval(100);
    11. connect(&m_timer, SIGNAL(timeout()), SLOT(updateEvents()));
    12. m_timer.start();
    13. }
    14.  
    15. private slots:
    16. void updateEvents()
    17. {
    18. m_timer.start();
    19. qApp->processEvents();
    20. }
    21. private:
    22. QTimer m_timer;
    23. };
    24. #include "main.moc"
    25. int main(int argc, char **argv)
    26. {
    27. QApplication app(argc, argv);
    28. MyProcessEventDispatcher mpee;
    29. ...
    30. return app.exec();
    31. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by spirit; 18th February 2009 at 11:24.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  12. #12
    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: thread problem

    Hi,

    Qt Code:
    1. #include <QThread>
    2. class myThread : public QThread
    3. {
    4. Q_OBJECT
    5. public:
    6. //Redefine this method with your computation
    7. void run()
    8. {
    9. while(!bStop)
    10. {
    11. //Do some work
    12. emit (emitShowText(QString("Hello Thread"));
    13. ...
    14. }
    15.  
    16. }
    17.  
    18. signals:
    19. void emitShowText(QString); //Define all the signals you want to emit
    20. void emitShowProcessProgress(int);
    21. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. myAppWindow::myAppWindow(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags)
    2. {
    3. ui.setupUi(this); //Setup UI if used a GUI editor like QDesigner
    4.  
    5. //Create the thread and connect it's signals to "this" slots
    6. m_pThread = new myThread(this);
    7. bool bC = connect(m_pThread,SIGNAL(emitShowText(QString)),this,SLOT(showText(QString)),Qt::QueuedConnection); //Insert a debug point and take a look at console out. If connection fail it will reach error message on it
    8.  
    9. m_pThread->start(); //Enter the Thread "run" method
    10. }
    To copy to clipboard, switch view to plain text mode 
    Òscar Llarch i Galán

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

    talk2amulya (18th February 2009)

  14. #13
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: thread problem

    for some reason

    Qt Code:
    1. connect(&m_timer, SIGNAL(timeout()), SLOT(updateEvents()));
    To copy to clipboard, switch view to plain text mode 

    isnt getting connected..

  15. #14
    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: thread problem

    I've updated code and also tested it. a slot is called normal.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  16. The following user says thank you to spirit for this useful post:

    talk2amulya (18th February 2009)

  17. #15
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: thread problem

    yeh, i made those changes too but still updateEvents() is only getting called once, until whole of the processing is done..so no help from this either..thanks though, spirit...

  18. #16
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: thread problem

    actually there is only one function call(which makes further calls) that is doing all the heavy processing..and that function call is in main()..perhaps the control isnt going back to the other objects or timers cuz of it..could that be true? just cuz one particular function has to execute, the control will go nowhere else?

  19. #17
    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: thread problem

    yes this is possible. so, try to call processEvents in that "havy" function.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  20. #18
    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: thread problem

    Hi,

    Calling "processEvents" is not a good idea. I think if you call it many times it ignore some of them.

    Have you ported your heavy computation inside the thread? You will not need to call "processEvents" then. You emit SIGNALs and let the main thread do its work.
    Òscar Llarch i Galán

  21. #19
    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: thread problem

    maybe this can help you Keeping the GUI Responsive
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  22. #20
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: thread problem

    that is EXACTLY what i did sm days back..and the icon animates cuz of it as desired..although intermittently ..but i had to place like 30 calls to processEvents() here and there inside it..and if i want it to be done smoothly i might need more..so thats why i was wondering there should be a better way to do it in QT..so i went with using timers and on timeout, calling processEvents(), but evidently it also has no effect..which i find really surprising...anyone has anymore info about processEvents()??or smth else that could resolve this issue?

Similar Threads

  1. thread - right approach?
    By heinz32 in forum Qt Programming
    Replies: 3
    Last Post: 17th June 2008, 17:39
  2. GUI thread and Working thread comunication
    By FasTTo in forum Qt Programming
    Replies: 2
    Last Post: 13th September 2007, 15:31
  3. Terminating a thread.
    By kiranraj in forum Qt Programming
    Replies: 3
    Last Post: 9th July 2007, 11:14
  4. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49
  5. Thread Problem
    By qball2k5 in forum Qt Programming
    Replies: 2
    Last Post: 12th April 2006, 17:31

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.