sending message to the client using Qt
-
@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!