PDA

View Full Version : my SLOT not part of QThread



freak
16th June 2006, 19:29
Hello, I got the following problem.



class mythread : public QThread
{
public:
mythread();
void run();
public slots:
void processpacket();

private:
QTcpServer *tcpServer;
};


inside mythread::run I have this



connect(tcpServer, SIGNAL(newConnection()), this, SLOT(processpacket()));


everything compiles and builds but when I run it I get a


Object::connect: No such slot QThread::processpacket().


and if I add Q_OBJECT as follows:



class mythread : public Qthread
{
Q_OBJECT

public:
mythread();
void run();
public slots:
void processpacket();

private:
QTcpServer *tcpServer;
};


I get the following error when compiling.:

undefined reference to 'vtable for mythread'

Any help in solving this issues is appreciated. Thanks in advanced

jpn
16th June 2006, 19:33
Re-run qmake after adding Q_OBJECT macro.

freak
16th June 2006, 19:45
Thanks Jpn, that solved both problems. Now i have a problem where the SLOT code is never executed. I send packets from my client machine but it does nothing. is the code shown above incorrect?

Thanks in advanced

jacek
16th June 2006, 19:52
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(processpacket()));
Beware that a QThread object and all objects created in its constructor live in a thread where that QThread instance was created, while all objects created in QThread::run() live in a newly created thread.

This means that the above connection will be a queued connection and processpacket() won't be executed by the new thread. Either move those slots to some other object that you will create in QThread::run() or make that connection a direct one explicitly.

jacek
16th June 2006, 19:54
Now i have a problem where the SLOT code is never executed.
Do you start the event loop in that new thread (i.e. do you invoke QThread::exec() in run() )?

freak
16th June 2006, 19:59
I do call exec() inside run.

How do you make a connection explicitly?

jacek
16th June 2006, 20:17
How do you make a connection explicitly?

connect( tcpServer, SIGNAL( newConnection() ),
this, SLOT( processpacket() ),
Qt::DirectConnection );