0

I am having trouble to connect a Signal to a Slot in the following code:

#include "myserver.h"

MyServer::MyServer(QObject *parent) :
    QTcpServer(parent)
{
}

void MyServer::StartServer()
{
    if(listen(QHostAddress::Any, 45451))
    {
        qDebug() << "Server: started";
        emit servComando("Server: started");
    }
    else
    {
        qDebug() << "Server: not started!";
        emit servComando("Server: not started!");
    }
}

void MyServer::incomingConnection(int handle)
{
    emit servComando("server: incoming connection, make a client...");

    // at the incoming connection, make a client
    MyClient *client = new MyClient(this);
    client->SetSocket(handle);

    //clientes.append(client);
    //clientes << client;

    connect(client, SIGNAL(cliComando(const QString&)),this, SLOT(servProcesarComando(const QString&)));

    // para probar
    emit client->cliComando("prueba");

}

void MyServer::servProcesarComando(const QString& texto)
{
    emit servComando(texto);
}

The emit client->cliComando("prueba"); works, but the real "emits" don't. The console does not show any connection error, and the QDebug texts shows everything works well. Original code was copied from http://www.bogotobogo.com/cplusplus/sockets_server_client_QT.php

Louis Langholtz
  • 2,913
  • 3
  • 17
  • 40
  • Show us your signal handling code. – arrowd Aug 30 '17 at 06:33
  • _The emit client->cliComando("prueba"); works_ How do you know this? Also, you can simply connect SIGNAL to SIGNAL: _connect(client, SIGNAL(cliComando(const QString&)),this, SIGNAL(servComando(const QString&)));_ – Xplatforms Aug 30 '17 at 07:21
  • Also _"prueba"_ used as an argument is not obviously _const QString&_ it depends on compiler and Qt switches used. Use _QStringLiteral("prueba")_ And sett SIGNAL/SLOT argument to _const QString_ if you insert argument directly to the function body and not sure if instantiated param in QMeta process still available and has value – Xplatforms Aug 30 '17 at 07:30
  • 1
    My magic ball says: missing macro `Q_OBJECT` in definition of class `MyServer`. – Marek R Aug 30 '17 at 08:28
  • offtopic: why you are inheriting `QTcpServer`. Here composition should be used. – Marek R Aug 30 '17 at 08:30
  • Your code snippet is missing the test cases. You should provide a complete [mcve] if you expect useful answers. – Murphy Aug 30 '17 at 10:51

1 Answers1

0

I found the problem, Im sending a signal BEFORE connecting:

client->SetSocket(handle);

sends the signal, and Im CONNECTing after it... Now it is:

// at the incoming connection, make a client
MyClient *client = new MyClient(this);

connect(client, SIGNAL(cliComando(const QString&)),this, SLOT(servProcesarComando(const QString&)));

client->SetSocket(handle);

And it works. I noticed it after read the following:

13. Put all connect statements before functions calls that may fire their signals, to ensure that the connections are made before the signals are fired. For example:

_myObj = new MyClass();
connect(_myObj, SIGNAL(somethingHappend()), SLOT(doSomething()));
_myObj->init();

not

_myObj = new MyClass();
_myObj->init();
connect(_myObj, SIGNAL(somethingHappend()), SLOT(doSomething()));

I found it at https://samdutton.wordpress.com/2008/10/03/debugging-signals-and-slots-in-qt/

Anyway, thanks for your answers!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241