dkoryagin
26th September 2010, 13:18
Hi all :) I have another problem. maybe someone seen this before....
I'm using QSerialDevice lib to communicate with GPS reciever over COM port. All work with this lib i've moved to thread.
Very very simplified class looks like this:
header:
class testThread : public QThread
{
Q_OBJECT
public:
testThread(){};
protected:
virtual void run();
private slots:
void trigged();
private:
AbstractSerial *as;
};
cpp:
void testThread::run()
{
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(trigged()));
timer->start(10000);
as = new AbstractSerial(this);
as->setDeviceName("COM3");
exec(); // begin eventloop
}
void testThread::trigged()
{
if (!as->isOpen())
{
if (as->open(AbstractSerial::ReadOnly))
{
QMessageBox *msg = new QMessageBox();
msg->setText( " opened ");
msg->show();
}
}
}
Thread starts, after 10 seconds timer emit signal timeout and all GUI freeze...:( After it leave trigged func everything woks normal until next emit
Any ideas? Problem in lib or ...?
update:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is testThread(0xba2af8), parent's thread is QThread(0xad7938), current thread is testThread(0xba2af8)
this was in console. What does it mean?
update: :D
problem solved:
testThread thrd = new testThread();
thrd->moveToThread(thrd);
is it correct?
I'm using QSerialDevice lib to communicate with GPS reciever over COM port. All work with this lib i've moved to thread.
Very very simplified class looks like this:
header:
class testThread : public QThread
{
Q_OBJECT
public:
testThread(){};
protected:
virtual void run();
private slots:
void trigged();
private:
AbstractSerial *as;
};
cpp:
void testThread::run()
{
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(trigged()));
timer->start(10000);
as = new AbstractSerial(this);
as->setDeviceName("COM3");
exec(); // begin eventloop
}
void testThread::trigged()
{
if (!as->isOpen())
{
if (as->open(AbstractSerial::ReadOnly))
{
QMessageBox *msg = new QMessageBox();
msg->setText( " opened ");
msg->show();
}
}
}
Thread starts, after 10 seconds timer emit signal timeout and all GUI freeze...:( After it leave trigged func everything woks normal until next emit
Any ideas? Problem in lib or ...?
update:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is testThread(0xba2af8), parent's thread is QThread(0xad7938), current thread is testThread(0xba2af8)
this was in console. What does it mean?
update: :D
problem solved:
testThread thrd = new testThread();
thrd->moveToThread(thrd);
is it correct?