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
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QMainWindow>
- namespace Ui {
- class MainWindow;
- }
- {
- Q_OBJECT
- public:
- ~MainWindow();
- private:
- Ui::MainWindow *ui;
- };
- #endif // MAINWINDOW_H
mainwindow.cpp
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- }
- MainWindow::~MainWindow()
- {
- delete ui;
- }
main.cpp
- #include <QtGui/QApplication>
- #include "mainwindow.h"
- int main(int argc, char *argv[])
- {
- MainWindow w;
- w.show();
- return a.exec();
- }
And a MyThread class like this
- class MyThread
- {
- public:void run();
- };
- void MyThread::run()
- {
- }
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
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
You can look at the examples: here [doc.qt.nokia.com]
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] ?
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
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).
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
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…
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
You must log in to post a reply. Not a member yet? Register here!






