PDA

View Full Version : Emit Signal not working for thread



anh5kor
18th September 2017, 13:00
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

QtGUiApplication.cpp


QtGuiApplication::QtGuiApplication(QWidget *parent)
: QMainWindow(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();
}


LoggerThread.cpp


LoggerThread::LoggerThread(QObject *parent) :
QThread(parent)
{

}
void LoggerThread::run()
{
while(1)
{
ClientThread *thr = ClientThread(serverSocket.accept());
thr->start();
}
}


ClientThread.cpp (third party 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.


QtGUiApplication.cpp


QtGuiApplication::QtGuiApplication(QWidget *parent)
: QMainWindow(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);
}


LoggerThread.cpp


LoggerThread::LoggerThread(QObject *parent) :
QThread(parent)
{

}
void LoggerThread::run()
{
while(1)
{
ClientThread *thr = ClientThread(serverSocket.accept(), reaper);
thr->start();
}
void LoggerThread::CallbackData(string value) // added new function
{
QString result = QString::fromStdString(value);
emit SendResult(result); //where SendResult(const QString &result) is declear as signals: in .h file
}
}


ClientThread.cpp (third party 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.

GeneCode
19th September 2017, 01:02
Ah that's easy.
Buy a book about Qt and learn it.

anh5kor
20th September 2017, 13:29
Can anyone have the better suggestion for my Query?

high_flyer
21st September 2017, 10:54
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:



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();
}
}