Emit Signal not working for thread
Hello
I have integrated a third-party code in Qt which is running in different thread apart from the main thread.The third party code opens a port and reads a data from the connected client via the socket and I have to display all those data on my Ui the third party code does not have any Qt application background.I have created a thread which run the third party application without disturbing my main thread.
But the problem is displaying the read data from the third party to my UI.I am writing the below prototype code for more understanding
Quote:
QtGUiApplication.cpp
Code:
QtGuiApplication
::QtGuiApplication(QWidget *parent
){
ui.setupUi(this);
LoggerThread *thread = new LoggerThread;
QObject::connect(ui.
buttonStart,
SIGNAL(clicked
(bool)),
this,
SLOT(runthread
()));
}
void QtGuiApplication1::runthread() //where runthread() is declear a private slots in .h file
{
thread->start();
}
Code:
LoggerThread
::LoggerThread(QObject *parent
) :{
}
void LoggerThread::run()
{
while(1)
{
ClientThread *thr = ClientThread(serverSocket.accept());
thr->start();
}
}
Quote:
ClientThread.cpp (third party code)
Code:
ClientThread::ClientThread(Socket clientstock_):Clientsock(clientstock_)
{
cout << "Received a client connection!!!!" << endl;
}
void ClientThread::run()
{
while (1)
{
if (!clientsock.isOpen())
{
return;
}
SocketBuffer msgSizeBuffer(sizeof(unsigned int));
if (!clientsock.read(msgSizeBuffer))
{
return;
}
unsigned int msgSize = msgSizeBuffer.readInt();
SocketBuffer buffer(msgSize);
if (!clientsock.read(buffer))
{
return;
}
spi::InternalLoggingEvent event = readFromBuffer(buffer);
string value = event.getMessage(); //This value i have to display on TextEdit
}
}
I tried emitting the signals to get the value on my Ui, find the below code.
Quote:
QtGUiApplication.cpp
Code:
QtGuiApplication
::QtGuiApplication(QWidget *parent
){
ui.setupUi(this);
LoggerThread *thread = new LoggerThread;
QObject::connect(ui.
pb_start,
SIGNAL(clicked
(bool)),
this,
SLOT(runthread
()));
}
void QtGuiApplication1::runthread() //where runthread() is declear a private slots in .h file
{
QObject::connect(thread,
SIGNAL(SendResult
(QString)),
this,
SLOT(Displaydata
(QString)));
//new Line added thread->start();
}
void QtGuiApplication1
::Displaydata(const QString &data1
) //where Displaydata(const QString &) is declear a private slots in .h file,added new function {
ui.lineEdit->setText(data1);
}
Code:
LoggerThread
::LoggerThread(QObject *parent
) :{
}
void LoggerThread::run()
{
while(1)
{
ClientThread *thr = ClientThread(serverSocket.accept(), reaper);
thr->start();
}
void LoggerThread::CallbackData(string value) // added new function
{
emit SendResult(result); //where SendResult(const QString &result) is declear as signals: in .h file
}
}
Quote:
ClientThread.cpp (third party code)
Code:
LoggerThread data;
ClientThread::ClientThread(Socket clientstock_):Clientsock(clientstock_)
{
cout << "Received a client connection!!!!" << endl;
}
void ClientThread::run()
{
while (1)
{
if (!clientsock.isOpen())
{
return;
}
SocketBuffer msgSizeBuffer(sizeof(unsigned int));
if (!clientsock.read(msgSizeBuffer))
{
return;
}
unsigned int msgSize = msgSizeBuffer.readInt();
SocketBuffer buffer(msgSize);
if (!clientsock.read(buffer))
{
return;
}
spi::InternalLoggingEvent event = readFromBuffer(buffer);
string value = event.getMessage(); //This value has to be display on TextEdit
data.CallbackData(s);// added new line
}
}
But i found that emit SendResult(result) is not getting emitted and my slot is not getting called.
Re: Emit Signal not working for thread
Ah that's easy.
Buy a book about Qt and learn it.
Re: Emit Signal not working for thread
Can anyone have the better suggestion for my Query?
Re: Emit Signal not working for thread
Your code is a mess on so many levels its hard to know where to start.
It should not even compile as in LoggerThread::run() this code is illegal:
Code:
void LoggerThread::run()
{
while(1)
{
ClientThread *thr = ClientThread(serverSocket.accept()); // you are assigning a stack copy initialization to a pointer. This should not compile, and if it did, it should crash.
thr->start();
}
}