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;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private:
Ui::MainWindow *ui;
};#endif // MAINWINDOW_H
@mainwindow.cpp
@
#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
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[])
{
QApplication a(argc, 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]
-
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.
-
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 -
-
@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?
-
You can look at the examples: "here":http://doc.qt.nokia.com/4.7/network-network-chat.html
-
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":http://doc.qt.nokia.com/4.7/qthread.html#details ?
-
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 -
[quote]and my client should always be in listen mode,it should be listening for messages from other clients.[/quote]
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).
-
[quote author="peppe" date="1292837082"]
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).[/quote]Really good article :D
-
[quote author="Il Rugginoso" date="1292836477"] http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/[/quote] Thanks for the tipp! I didn't know the concepts behind the QThread class...
-
[quote author="Wolf P." date="1292842687"][quote author="Il Rugginoso" date="1292836477"] http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/[/quote] Thanks for the tipp! I didn't know the concepts behind the QThread class...
[/quote]You're welcome :D