繁体   English   中英

QT连接插槽/信号不起作用

[英]QT Connect Slot / Signal not working

我无法通过以下代码将信号连接到插槽:

#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);
}

emit client->cliComando("prueba"); 有效,但真正的“发射”却不起作用。 控制台没有显示任何连接错误,并且QDebug文本显示一切正常。 原始代码是从http://www.bogotobogo.com/cplusplus/sockets_server_client_QT.php复制而来的

我发现了问题,我在连接之前发送了一个信号:

client->SetSocket(handle);

发送信号,并在其后连接Im ...现在是:

// 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);

而且有效。 阅读以下内容后,我注意到了它:

13.将所有connect语句放在可能会触发其信号的函数调用之前,以确保在触发信号之前进行连接。 例如:

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

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

我在https://samdutton.wordpress.com/2008/10/03/debugging-signals-and-slots-in-qt/中找到了它

无论如何,谢谢您的回答!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM