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
::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();
}
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();
}
To copy to clipboard, switch view to plain text mode
LoggerThread
::LoggerThread(QObject *parent
) :{
}
void LoggerThread::run()
{
while(1)
{
ClientThread *thr = ClientThread(serverSocket.accept());
thr->start();
}
}
LoggerThread::LoggerThread(QObject *parent) :
QThread(parent)
{
}
void LoggerThread::run()
{
while(1)
{
ClientThread *thr = ClientThread(serverSocket.accept());
thr->start();
}
}
To copy to clipboard, switch view to plain text mode
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
}
}
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
}
}
To copy to clipboard, switch view to plain text mode
I tried emitting the signals to get the value on my Ui, find the below 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);
}
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);
}
To copy to clipboard, switch view to plain text mode
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
}
}
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
}
}
To copy to clipboard, switch view to plain text mode
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
}
}
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
}
}
To copy to clipboard, switch view to plain text mode
But i found that emit SendResult(result) is not getting emitted and my slot is not getting called.
Bookmarks