aladin8848 aladin8848 - 1 year ago 218
C++ Question

Qt websocket send message and wait for response before going to next method

I am working with Qt websockets.

Wanted to ask you, do you know how could I make this communication:

for example I have this two methods and I would like to send message to server, than wait for response (because it refreshes the

variable) and than proccess


Sure I could use Signal and Slots... but dont know how could I use it here, see:

when I go
ok it will send message to server

void Client::sendMessageToServer(QString &str){


what should I emit than? I have no idea how to be in waiting state... there is no such a slot in
, it just go to method
It doesn't go here where the data are set:
btw I have setup client like this when it recieves message it goes to
because of this:

connect(&m_wSocket, &QWebSocket::textMessageReceived, this, &Client::onTextMessageReceived);

void Client::onTextMessageReceived(QString message)

Do you have any idea?

Answer Source

You can emit a signal on setData or onTextMessageReceived and connect to this.

void Client::setData(QString message)
    //... whatever you do in setData

    emit dataChanged();       

in the other class you then can do

connect(client, &Client::dataChanged,this,&<...>::myMethod);

then just call

//without the myMethod call

And whenever setData will be called myMethod will automatically be called too.

Qt is not meant to be "waiting" most of the times. It is event driven. You define an event (or use one already existing) and tell the program what to do when this event happens.

It is a bit unusual compared to "normal" programming but you get used to it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download