Results 1 to 20 of 22

Thread: thread problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. #2
    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!

  3. #3
    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

  4. #4
    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....

  5. #5
    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].

  6. #6
    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..

  7. #7
    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].

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

    talk2amulya (18th February 2009)

  9. #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

    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...

  10. #9
    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?

  11. #10
    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].

  12. #11
    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

  13. #12
    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].

  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

    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?

  15. #14
    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 have already read that link..and i have tried sm of the ways that fits my needs..but none of them worked..

  16. #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

    hi oscar,

    the heavy processing code contains some GUI calls, so i cant put that in another thread.. even if it was to be done..it'd be a lot of work..i m looking for a solution that bypasses me having to change anything in the way the things are done currently

  17. #16
    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

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

    talk2amulya (18th February 2009)

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.