sending message to the client using Qt
-
I have made a UI of multi threaded server in C++ using Qt . The server is receiving the messages as soon as the client sends a message and it is handling all the clients at the same time. Now I want the server to send the message when I click on 'send' button and it should send the message to all the clients at the same time but I am not able to do that I have used readyread() function for receiving the messages. I want the server to send the message that I enter in the textbox as soon as I click on the send button. how can I do that? is there any pre defined function like readyread() for sending messages as well?
-
@fari35 There are various write methods: https://doc.qt.io/qt-5/qiodevice.html#write
You also did not mention how you actually communicate? Networking? -
@fari35 said in sending message to the client using Qt:
send the message to all the clients at the same time
If you are not already aware, this will require you to write the message to each & every connected client separately in a loop. There is no "write data to all clients in one call" method.
-
@JonB No I mean the server assigns a separate thread to each client and readyread() function is a predefined function in Qthread and it is executed in each thread separately. I am following the this example: https://www.bogotobogo.com/Qt/Qt5_QTcpServer_Multithreaded_Client_Server.php
so I want a function for sending the message just like readyread isfor receiving -
so I want a function for sending the message just like readyread isfor receiving
readyRead
is a signal, to indicate data has arrived. There is no "signal" in the same way for sending data, because there is nothing to signal. You just usewrite()
as @jsulm has said.That is a separate matter from, perhaps, inventing your own signal out of the UI thread to instruct the other threads socketed to the clients that now is the time to send their data to the clients. But this will emanate from the signal on pressing the button in the UI, in the slot you choose to attach to that.
-
@JonB I'm trying to make a slot in my fthread.cpp file and a signal in fserver file which will be connected to the signal of mainwindow.cpp, so that as soon as I'll emit the signal from mainwindow.cpp file it will execute the slot in fthread.cpp file. But here I'm not able to make an object of fserver class in fthread class, so how can I do this ?
I have also attached my code in the question -
@fari35
You won't "make an object of fserver class in fthread class". Just connect a signal out of the UI on button press to the slot in the thread to send the message to the client. As I wrote earlier, you'll have to do theconnect()
for each thread-client.Do not explicitly write the
Qt::DirectConnection
s I see you have. Leave Qt to determine connection types (omit the parameter). When connecting across threads it will useQt::QueuedConnection
, which is what is required.Don't try to specify a
&MainWindow::sendmessage
in any non-MainWindow
class. Make connections in a class which knows about both the sending-thread instance and receiving-thread instance. If necessary, declare your own signal not inMainWindow
class but rather in server thread class. It is permissible for a class toemit
a signal which is declared in another class. -
@JonB I am trying to connect the signal in mainwindow with the slot in fthread like this:
connect(mainobj, &MainWindow::sendmessage,this, &fthread::sendmessage);
but I am not able to make the mainwindow object in fthread class. Is there any alternate way of doing this?
-
@fari35
I just answered that in my final paragraph above.fthread
knows nothing about eitherMainWindow
ormainobj
. One solution is to do this connection in theMainWindow
instance, where main window knows about eachfthread
instance. Another solution is to define the signal infthread
class and have main window goemit fthreadIntance->fthreadSignal()
. -
-
@fari35 said in sending message to the client using Qt:
I cannot connect them in mainwindow class.
That is indeed where you should do the connection! It should connect the button press to a slot in each thread you create.
I never allow
#include "mainwindow.h"
in any file other thanmainwindow.cpp
. Other modules should know nothing about a main window, whether it exists or not.Have the thread class export a slot. Include that into
mainwindow.cpp
. When a thread instance is created, have main windowconnect()
its button signal to the thread slot, for each thread. (You can also create a custom signal wrapper if it's neater.) If necessary, have the thread emit a signal when it is created so that the main window knows to do the connection.In a word, the main window can know about threads, but the threads should not know about the main window. Therefore the
connect()
s should not be in the threads but rather in the main window. -
@JonB I have done this but now When I emit a signal sendmessage() from mainwindow class (sendmessage() signal is working perfectly fine) to the slot of thread class so the thread class does not emit the gotnewmessage() signal when I want to send the data to mainwindow class that it needs to show in text box. Do you hav any idea why is that happening?
-
This post is deleted!