簡體   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