
Originally Posted by
franz
This means that you have to define a
QObject inside the run() function; one that actually lives in another thread. Then you can use the timer to tell that
QObject to doAction().
Ok, i've rewrite it, so at the moment it looks like this
/* Thread.h */
#include <QThread>
{
Q_OBJECT
public:
Object();
protected:
signals:
void update(int);
private:
int timerId;
}
{
Q_OBJECT
public:
Thread();
void run();
private:
Object* object;
};
/* Thread.h */
#include <QThread>
class Object : public QObject
{
Q_OBJECT
public:
Object();
protected:
void timerEvent(QTimerEvent*);
signals:
void update(int);
private:
int timerId;
}
class Thread : public QThread
{
Q_OBJECT
public:
Thread();
void run();
private:
Object* object;
};
To copy to clipboard, switch view to plain text mode
/* Thread.cpp */
#include "Thread.h"
Thread::Thread() {
}
void Thread::run() {
object = new Object();
exec();
}
Object::Object() {
timerId = startTimer(1000);
}
Object::~Object() {
killTimer(timerId);
}
here i write some data to port and then get some data
emit update(some data);
}
/* Thread.cpp */
#include "Thread.h"
Thread::Thread() {
}
void Thread::run() {
object = new Object();
exec();
}
Object::Object() {
timerId = startTimer(1000);
}
Object::~Object() {
killTimer(timerId);
}
void Object::timerEvent(QTimerEvent* event) {
here i write some data to port and then get some data
emit update(some data);
}
To copy to clipboard, switch view to plain text mode
As i understood, you advised me to create an object inside QThread::run() function and then to use a timer for this object. Unfortunately, GUI is steel freezing when this object is trying to read from port. Maybe, i've missed something?
Bookmarks