Results 1 to 4 of 4

Thread: Emit Signal not working for thread

  1. #1
    Join Date
    Oct 2014
    Posts
    71
    Thanks
    13
    Qt products
    Qt5
    Platforms
    Windows

    Question 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
    QtGUiApplication.cpp
    Qt Code:
    1. QtGuiApplication::QtGuiApplication(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. ui.setupUi(this);
    5. LoggerThread *thread = new LoggerThread;
    6. QObject::connect(ui.buttonStart, SIGNAL(clicked(bool)), this, SLOT(runthread()));
    7. }
    8.  
    9. void QtGuiApplication1::runthread() //where runthread() is declear a private slots in .h file
    10. {
    11. thread->start();
    12. }
    To copy to clipboard, switch view to plain text mode 
    LoggerThread.cpp
    Qt Code:
    1. LoggerThread::LoggerThread(QObject *parent) :
    2. QThread(parent)
    3. {
    4.  
    5. }
    6. void LoggerThread::run()
    7. {
    8. while(1)
    9. {
    10. ClientThread *thr = ClientThread(serverSocket.accept());
    11. thr->start();
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 
    ClientThread.cpp (third party code)
    Qt Code:
    1. ClientThread::ClientThread(Socket clientstock_):Clientsock(clientstock_)
    2. {
    3. cout << "Received a client connection!!!!" << endl;
    4. }
    5. void ClientThread::run()
    6. {
    7. while (1)
    8. {
    9. if (!clientsock.isOpen())
    10. {
    11. return;
    12. }
    13. SocketBuffer msgSizeBuffer(sizeof(unsigned int));
    14. if (!clientsock.read(msgSizeBuffer))
    15. {
    16. return;
    17. }
    18. unsigned int msgSize = msgSizeBuffer.readInt();
    19. SocketBuffer buffer(msgSize);
    20. if (!clientsock.read(buffer))
    21. {
    22. return;
    23. }
    24. spi::InternalLoggingEvent event = readFromBuffer(buffer);
    25. string value = event.getMessage(); //This value i have to display on TextEdit
    26.  
    27. }
    28. }
    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.cpp
    Qt Code:
    1. QtGuiApplication::QtGuiApplication(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. ui.setupUi(this);
    5. LoggerThread *thread = new LoggerThread;
    6. QObject::connect(ui.pb_start, SIGNAL(clicked(bool)), this, SLOT(runthread()));
    7. }
    8.  
    9. void QtGuiApplication1::runthread() //where runthread() is declear a private slots in .h file
    10. {
    11. QObject::connect(thread, SIGNAL(SendResult(QString)), this, SLOT(Displaydata(QString))); //new Line added
    12. thread->start();
    13. }
    14. void QtGuiApplication1::Displaydata(const QString &data1) //where Displaydata(const QString &) is declear a private slots in .h file,added new function
    15. {
    16. ui.lineEdit->setText(data1);
    17. }
    To copy to clipboard, switch view to plain text mode 
    LoggerThread.cpp
    Qt Code:
    1. LoggerThread::LoggerThread(QObject *parent) :
    2. QThread(parent)
    3. {
    4.  
    5. }
    6. void LoggerThread::run()
    7. {
    8. while(1)
    9. {
    10. ClientThread *thr = ClientThread(serverSocket.accept(), reaper);
    11. thr->start();
    12. }
    13. void LoggerThread::CallbackData(string value) // added new function
    14. {
    15. QString result = QString::fromStdString(value);
    16. emit SendResult(result); //where SendResult(const QString &result) is declear as signals: in .h file
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 
    ClientThread.cpp (third party code)
    Qt Code:
    1. LoggerThread data;
    2. ClientThread::ClientThread(Socket clientstock_):Clientsock(clientstock_)
    3. {
    4. cout << "Received a client connection!!!!" << endl;
    5. }
    6. void ClientThread::run()
    7. {
    8. while (1)
    9. {
    10. if (!clientsock.isOpen())
    11. {
    12. return;
    13. }
    14. SocketBuffer msgSizeBuffer(sizeof(unsigned int));
    15. if (!clientsock.read(msgSizeBuffer))
    16. {
    17. return;
    18. }
    19. unsigned int msgSize = msgSizeBuffer.readInt();
    20. SocketBuffer buffer(msgSize);
    21. if (!clientsock.read(buffer))
    22. {
    23. return;
    24. }
    25. spi::InternalLoggingEvent event = readFromBuffer(buffer);
    26. string value = event.getMessage(); //This value has to be display on TextEdit
    27. data.CallbackData(s);// added new line
    28.  
    29. }
    30. }
    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.

  2. #2
    Join Date
    Sep 2017
    Posts
    29
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: Emit Signal not working for thread

    Ah that's easy.
    Buy a book about Qt and learn it.

  3. #3
    Join Date
    Oct 2014
    Posts
    71
    Thanks
    13
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Emit Signal not working for thread

    Can anyone have the better suggestion for my Query?

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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:

    Qt Code:
    1. void LoggerThread::run()
    2. {
    3. while(1)
    4. {
    5. 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.
    6. thr->start();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Qt thread emit signal and the slot is fired later
    By dragonprog in forum Qt Programming
    Replies: 1
    Last Post: 14th March 2017, 15:00
  2. Seems that my emit doesn't emit the signal
    By roseicollis in forum Newbie
    Replies: 2
    Last Post: 19th January 2015, 17:05
  3. Emit signal from QThread to main thread!!!
    By sanujas in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 26th December 2013, 15:22
  4. Emit Signal to Thread and additional Forward
    By CLRS530 in forum Qt Programming
    Replies: 0
    Last Post: 23rd May 2010, 20:45
  5. Emit signal from thread
    By sisco in forum Newbie
    Replies: 2
    Last Post: 26th November 2009, 14:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.