December 20, 2010

endla.ravi endla.ravi
Lab Rat
6 posts

Problem with QThread..

 

hi..

I have a widget which is working fyn..

But in background an infinite loop should be there.When i put an infinite loop,the Widget becomes dark and finally crashes..

I think Threads can be used to solve this problem..(correct me if im wrong)

But i am unable to use QThread class ..for example if i have

mainwindow.h

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5.  
  6. namespace Ui {
  7.     class MainWindow;
  8. }
  9.  
  10. class MainWindow : public QMainWindow
  11. {
  12.     Q_OBJECT
  13.  
  14. public:
  15.     explicit MainWindow(QWidget *parent = 0);
  16.     ~MainWindow();
  17.  
  18. private:
  19.     Ui::MainWindow *ui;
  20. };
  21.  
  22. #endif // MAINWINDOW_H

mainwindow.cpp

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3.  
  4. MainWindow::MainWindow(QWidget *parent) :
  5.     QMainWindow(parent),
  6.     ui(new Ui::MainWindow)
  7. {
  8.     ui->setupUi(this);
  9. }
  10.  
  11. MainWindow::~MainWindow()
  12. {
  13.     delete ui;
  14. }

main.cpp

  1. #include <QtGui/QApplication>
  2. #include "mainwindow.h"
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.     QApplication a(argc, argv);
  7.     MainWindow w;
  8.     w.show();
  9.  
  10.     return a.exec();
  11. }

And a MyThread class like this

  1. class MyThread
  2. {
  3. public:void run();
  4.  
  5. };
  6.  
  7. void MyThread::run()
  8. {
  9. }

Now can anyone explain how to put the widget and while(1) {} loop in two different threads,so that both are running simultaneously…

Thanking u in advance

edit: Code highlighted / Denis Kormalev

13 replies

December 20, 2010

VCsala VCsala
Lab Rat
323 posts

Can you please use ‘@’ tags to make the code more readable?

December 20, 2010

VCsala VCsala
Lab Rat
323 posts

I am not sure you need threads, you can call processEvents from your infinite loop, which makes your GUI items responsive.

December 20, 2010

Denis Kormalev Denis Kormalev
Lab Rat
1607 posts

Please use @ tags for code. I edited your post, but in further posts please use it by yourself.

I don’t see any usage of your MyThread class. And it should be inherited from QThread if you want to use threading.

December 20, 2010

Smar Smar
Lab Rat
252 posts

Usually (mis)using threads causes gui going unresponsive…

Qt already have event loop, using own events and/or signal/slot system.

http://doc.qt.nokia.com/latest/signalsandslots.html
http://doc.qt.nokia.com/latest/eventsandfilters.html

December 20, 2010

Gerolf Gerolf
Hobby Entomologist
3251 posts

endla.ravi what do you want to do in your infinite loop? what is it needed or? Is it background processing (could be done in a thread) or shall it make event processing (is already done by return a.exec();)?

Please specify your needs a bit more.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

December 20, 2010

endla.ravi endla.ravi
Lab Rat
6 posts

@above,

I am actually coding for a messenger

and my client should always be in listen mode,it should be listening for messages from other clients.
But when the client i listening for messages,the client become crashes after some time.

How could i handle this?

December 20, 2010

Gerolf Gerolf
Hobby Entomologist
3251 posts

You can look at the examples: here [doc.qt.nokia.com]

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

December 20, 2010

Wolf P. Wolf P.
Ant Farmer
326 posts

You have to subclass MyThread from QThread then define in its run method what you want to do in the background. Did you read the basic thread example [doc.qt.nokia.com] ?

December 20, 2010

Il Rugginoso Il Rugginoso
Lab Rat
5 posts

Maybe in your case it’s better to use the QThread::run() default implementation that runs a local (for thread) event loop.
You simply have to create your QWidget and your QThread, than use QObject::moveToThread() method and call the QThread::start() method.

References:
http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/
http://doc.qt.nokia.com/4.7/qthread.html#start
http://doc.qt.nokia.com/4.7/qobject.html#moveToThread

December 20, 2010

peppe peppe
Ant Farmer
1005 posts
and my client should always be in listen mode,it should be listening for messages from other clients.

Please read my article here: http://developer.qt.nokia.com/wiki/ThreadsEventsQObjects . This seems to be the another case where threads are absolutely not needed (and indeed they just make your life a lot harder).

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

December 20, 2010

Il Rugginoso Il Rugginoso
Lab Rat
5 posts
peppe wrote:
Please read my article here: http://developer.qt.nokia.com/wiki/ThreadsEventsQObjects . This seems to be the another case where threads are absolutely not needed (and indeed they just make your life a lot harder).

Really good article :D

December 20, 2010

Wolf P. Wolf P.
Ant Farmer
326 posts

Il Rugginoso wrote:
http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/
Thanks for the tipp! I didn’t know the concepts behind the QThread class…

December 20, 2010

Il Rugginoso Il Rugginoso
Lab Rat
5 posts
Wolf P. wrote:
Il Rugginoso wrote:
http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/
Thanks for the tipp! I didn’t know the concepts behind the QThread class…

You’re welcome :D

 
  ‹‹ QPainter JNI call crashes the application      [SOLVED] Symbol(s) not found - template class ››

You must log in to post a reply. Not a member yet? Register here!