PDA

View Full Version : QSerialDevice cause GUI to freeze



dkoryagin
26th September 2010, 14: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?

wysota
26th September 2010, 16:19
No, you're doing it wrong (http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/). And after moveToThread() you'll be accessing GUI from non-gui thread.

dkoryagin
26th September 2010, 18:13
I think i've posted bad example of class :(
There is no GUI connection between threads...No messageboxes....Connection between gui and thread inplemented by signals.

Thanks, wysota.