PDA

View Full Version : QTcpServer connected signal does not invoke corresponding slot in GUI application!!!!



adie
28th September 2013, 02:20
I have tried so many things but still problem persists.

I can see that server is started and can connect using telnet. But it does not invoke new connection slot. What seems to be wrong?

main.cpp


int main(int argc, char *argv[])
{
core = new QGCCore(firstStart, argc, argv);
val = core->exec();
}


QGCCore.cpp:



QGCCore::QGCCore(bool firstStart, int &argc, char* argv[]) : QApplication(argc, argv),
restartRequested(false),
welcome(NULL)
{
Server s;
s.listen();
}


server.cpp


#include "server.h"
#include <QTcpServer>
#include <QTcpSocket>
#include <cstdio>
#include <QDebug>

Server::Server(QObject *parent) :
QObject(parent)
{
server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()),
this, SLOT(on_newConnection()));
qDebug() << "Server instance created";
}

void Server::listen()
{
server->listen(QHostAddress::Any, 1234);
qDebug() << "Server listening to 1234";
}

void Server::on_newConnection()
{
qDebug() << "New connection made!";
}

anda_skoa
28th September 2013, 10:05
By the time event processing starts there is no longer any slot to call, because the receiver object is already gone.

The Server object is created, its listen is called and then it is destroyed.

If you want your Server instance to be a signal receiver, don't destroy it, keep it :)

Cheers,
_

adie
28th September 2013, 23:14
Thanks anda_skoa so much..

I created a static singleton instance of server which stayed around..and signal/slot for new connection works like charm..

Newbie to c++, so still struggling with scopes...

:D

anda_skoa
29th September 2013, 11:46
It might have been easier to just add a Server member to QGCCore class :)
Or directly use the QTcpServer inside QGCore and connecting to its slots.

Cheers,
_