PDA

View Full Version : connec signal slot in thread problem



Teerayoot
24th December 2014, 07:32
Hi i got slot not found that i already declare


class MyReadThread :public QThread{

public:
QTcpSocket *ms;
MyReadThread(QTcpSocket*s){
ms = s;
connect(ms, SIGNAL(readyRead(void)), this, SLOT(on_socket_readyRead(void)));
}
void run(){
qDebug() << "mythread run";

}
private slots:

void on_socket_readyRead(void){
qDebug() << ms->readAll();
}

};
void qtnetwork::on_server_newConnection(void)
{
qDebug() << "new connection";
QTcpSocket *s = sv->nextPendingConnection();

MyReadThread *t = new MyReadThread(s);
t->start();


}
result:


QObject::connect: No such slot QThread::on_socket_readyRead(void) in qtnetwork.cpp:44
connected
mythread run
The thread 0x23a8 has exited with code 0 (0x0).

Lesiok
24th December 2014, 08:04
First MyReadThread class declaration must contain Directive Q_OBJECT.
Second without executing event loop thread will not receive signals from other threads. Method run should looks like :

void run(){
qDebug() << "mythread run";
QThread::run();
}

wysota
24th December 2014, 09:03
"t" object lives in the main thread therefore the slot will be executed in the main thread and not in the thread represented by "t". The rest of the code does not make sense either, the socket also lives in the main thread so overall the thread does exactly nothing.

Teerayoot
25th December 2014, 01:28
after i add Q_OBJECT to class i get new link error message


1>qtnetwork.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall MyReadThread::metaObject(void)const " (?metaObject@MyReadThread@@UBEPBUQMetaObject@@XZ)
1>qtnetwork.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall MyReadThread::qt_metacast(char const *)" (?qt_metacast@MyReadThread@@UAEPAXPBD@Z)
1>qtnetwork.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall MyReadThread::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@MyReadThread@@UAEHW4Call@QMetaObject @@HPAPAX@Z)
1>C:\Users\Jack\documents\visual studio 2013\Projects\qtnetwork\Win32\Debug\\qtnetwork.exe : fatal error LNK1120: 3 unresolved externals


i will improve logic later ,now need to solve problem why can't connect signal/slot.


Every Qt generate file are added


I might give up Qt and forgot programming Lol ,my brain can't handle Qt framework so complicate ?

jefftee
25th December 2014, 05:50
Just a guess but try running qmake, then try to rebuild. If that doesn't work, post your current version of code using the
and tags so that your code is properly formatted.

Teerayoot
25th December 2014, 08:32
#include "qtnetwork.h"
#include <QtNetwork\qtcpsocket.h>
#include <QtNetwork\qtcpserver.h>
#include <QtCore\qthread.h>
qtnetwork::qtnetwork(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);

//connect(ui.actionTest, SIGNAL(triggered(bool)), this, SLOT(on_actionTest_triggered(bool)));

}

qtnetwork::~qtnetwork()
{

}

void qtnetwork::on_actionTest_triggered(bool)
{
qDebug() << "hitme";




sv = new QTcpServer(this);
sv->listen(QHostAddress::LocalHost, 80);
connect(sv, SIGNAL(newConnection(void)), this, SLOT(on_server_newConnection(void)));

s = new QTcpSocket(this);
s->connectToHost("localhost", 80);
connect(s, SIGNAL(connected()), this, SLOT(on_socket_connect(void)));
connect(s, SIGNAL(readyRead()), this, SLOT(on_socket_readyread(void)));
//delete s;


}
class MyReadThread :public QThread{
Q_OBJECT
public:
QTcpSocket *ms;
MyReadThread(QTcpSocket*s){
ms = s;
connect(ms, SIGNAL(readyRead(void)), this, SLOT(on_socket_readyRead(void)));
}
void run(){
qDebug() << "mythread run";
QThread::run();
}
private slots:

void on_socket_readyRead(void){
qDebug() << ms->readAll();
}

};
void qtnetwork::on_server_newConnection(void)
{
qDebug() << "new connection";
QTcpSocket *s = sv->nextPendingConnection();

MyReadThread *t = new MyReadThread(s);
t->start();


}
void qtnetwork::on_socket_connect(void)
{
qDebug() << "connected";
const char buffer[] = { "Hello's world" };
int len = sizeof(buffer);
s->write(buffer, len);
}
void qtnetwork::on_socket_readyread(void)
{
qDebug() << s->readLine();
QByteArray t = s->readAll();
ui.plainTextEdit->appendPlainText("server:"+t);
}

wysota
25th December 2014, 09:17
You are still not using the thread for anything. All your objects live in the main thread. At line 78 you would get a crash if the slot executed in a worker thread. I really suggest you remove the thread completely (just replace QThread with QObject).

anda_skoa
25th December 2014, 11:07
after i add Q_OBJECT to class i get new link error message

As jthomps already said that can be fixed by re-running qmake.
This is only necessary because you added the Q_OBJECT marker after the header had been added to the project.

You can easily avoid this by always putting that marker in class declarations of QObject derived classes.

I also agree with wysota that you are making your life unnecessarily complicated by attempting too many things at once.
Just concentrate on the networking bits and keep threading for a later stage, if you need it at all.

Cheers,
_