Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QT return value from a signal
Forum Updated to NodeBB v4.3 + New Features

QT return value from a signal

Scheduled Pinned Locked Moved Solved Mobile and Embedded
5 Posts 2 Posters 1.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AMOL
    wrote on last edited by AMOL
    #1

    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)
    {

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

    }

    Please help me , thanks in advance

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • A Offline
        A Offline
        AMOL
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • A Offline
          A Offline
          AMOL
          wrote on last edited by AMOL
          #4

          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;
                  websocketPtr->close();
              });
          
              QJsonObject command;
              command.insert("command", "GET_IMAGES_WITH_INFO");
              QJsonDocument doc(command);
              QString strJson(doc.toJson(QJsonDocument::Compact));
              qInfo() << "Schedule::getImageWithInfo: Sending command " << strJson;
          
              websocketPtr->sendTextMessage(strJson);
          });
          
          websocket.open(QUrl(urlStr));
          
          QEventLoop eventLoop;
          QObject::connect(&websocket, SIGNAL(disconnected()), &eventLoop, SLOT(quit()));
          eventLoop.exec();
          
          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

          A 1 Reply Last reply
          0
          • A AMOL

            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;
                    websocketPtr->close();
                });
            
                QJsonObject command;
                command.insert("command", "GET_IMAGES_WITH_INFO");
                QJsonDocument doc(command);
                QString strJson(doc.toJson(QJsonDocument::Compact));
                qInfo() << "Schedule::getImageWithInfo: Sending command " << strJson;
            
                websocketPtr->sendTextMessage(strJson);
            });
            
            websocket.open(QUrl(urlStr));
            
            QEventLoop eventLoop;
            QObject::connect(&websocket, SIGNAL(disconnected()), &eventLoop, SLOT(quit()));
            eventLoop.exec();
            
            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

            A Offline
            A Offline
            AMOL
            wrote on last edited by
            #5

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

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved