c++ - qt qtcpserver is only working when it is created in main function -


i wrote simle qtcpserver, example: qt qtcpserver::incomingconnection(qintptr handle) not firing?

the server working, when created within main function.

working:

int main(int argc, char *argv[]) {     qcoreapplication a(argc, argv);      testserver server;      return a.exec(); } 

not working (this code won't work anyway this, show mean):

int main(int argc, char *argv[]) {     qcoreapplication a(argc, argv);      classa ca;      return a.exec(); }  classa::classa(qobject *parent) :     qthread(parent) {     testserver server; } 

when create class within main function , within create tcpserver, incomingconnection() function never executed, although server started. can see because "server start @ port ..." message a's constructor:

testserver::testserver(qobject *parent):     qtcpserver(parent) {     if (this->listen(qhostaddress::any, 2323)) {         qdebug() << "server start @ port: " << this->serverport();     } else {         qdebug() << "start failure";     } } 

that means, main function holds class , qtcpserver function. qtcpserver function again creates new thread when client connected. want have messages, received thread, in class a. why want create qtcpserver within class a.

does have suggestion this? how can create server within a?

thanks!

you need make object server member class classa, because object destroys after creation (because create in local scope)


Comments