Qt World Summit: Register Today!

QT return value from a signal

  • Hello developers,
    Need urgent help to solve one issue.
    I am creating client app which will connect to server, this server is Qwebsocket server.
    So when I connected I am sending data and receiving message in return .
    Now issue is when after connecting As per example I sued signals to connect and receive message.
    So how can we get that message in return from signal
    I am posting my function code here,

    bool plsqlserver::run(QString data)

    QUrl url=QStringLiteral("wss://localhost:2443");
    connect(&my_webSocket, &QWebSocket::connected,[this](){
        connect(&my_webSocket, &QWebSocket::textMessageReceived,
                [this](QString message){
            qDebug()<< "run returnvalue" << returnvalue; //*here my value appeard I want this value in return or atleast to use in this function*//
    connect(&my_webSocket, QOverload<const QList<QSslError>&>::of(&QWebSocket::sslErrors),
            this, &plsqlserver::onSslErrors);
    return returnvalue; <-----here no value is getting in return 


    Please help me , thanks in advance

  • Lifetime Qt Champion


    Signals have no return value.

    All your code is asynchronous so use that paradigm as well for the rest. Emit a signal once you've go the data you requested to propagate it further.

  • Hi @SGaist, I tried to emit signal at which set value in another function but and try get that value but before I get the value my function is completed and I got nothing in return
    Can you help me how to get that returnvalue value in my code at the end to return?

  • Also I found somthing in which he is getting message out side of signal, but I am not able to do the same for my case
    QJsonObject Schedule::getImageWithInfo()
    QString response;
    QString* responsePtr = &response;

    QWebSocket websocket;
    QWebSocket* websocketPtr = &websocket;
    QObject::connect(websocketPtr, &QWebSocket::connected, [&, websocketPtr, responsePtr]()
        qInfo() << "Schedule::getImageWithInfo: Connected";
        QObject::connect(websocketPtr, &QWebSocket::textMessageReceived, [websocketPtr, responsePtr](const QString& msg)
            qInfo() << "Schedule::getImageWithInfo: message received";
            *responsePtr = msg;
        QJsonObject command;
        command.insert("command", "GET_IMAGES_WITH_INFO");
        QJsonDocument doc(command);
        QString strJson(doc.toJson(QJsonDocument::Compact));
        qInfo() << "Schedule::getImageWithInfo: Sending command " << strJson;
    QEventLoop eventLoop;
    QObject::connect(&websocket, SIGNAL(disconnected()), &eventLoop, SLOT(quit()));
    QJsonObject responseObj = Utils::QJsonObjectFromString(response);
    return responseObj;


    Can you check this and let me know how I can use this same for my case
    Instead of jason object I want to use plain messgae

  • Somehow I managed to get value using single and made logic to send it to qml
    So issue is resolved
    Thank you for helping.....

Log in to reply