Results 1 to 3 of 3

Thread: MainWindow GUI cannot call SLOT in worker thread UDP object

  1. #1
    Join Date
    Mar 2018
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default MainWindow GUI cannot call SLOT in worker thread UDP object

    Hello,

    I am unable to emit() a signal from the MainWindow object to a worker thread that will send & receive UDP Data.
    The goal is to learn how to communicate from the GUI thread to the UDP thread.
    ***I do not understand why the SLOT is not working. I used the Mandlebrot example but in reverse, by communicating from GUI to thread. I tried QT4 & QT5 connect(signal, slot) functions without success. I get zero errors/warnings @ compile time***

    Mandlebrot example:
    Qt Code:
    1. connect(&thread, SIGNAL(renderedImage(QImage,double)), this, SLOT(updatePixmap(QImage,double)));
    To copy to clipboard, switch view to plain text mode 

    Mine in mainwindow.cpp:
    Qt Code:
    1. connect( this, SIGNAL(&MainWindow::runInitializeInthreadUDP), threadObj, SLOT(&MyUDP::Initialize), Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 

    They only message I get from the "Application Output" is:
    Qt Code:
    1. QObject::connect: Parentheses expected, signal MainWindow::&MainWindow::runInitializeInthreadUDP in mainwindow.cpp:36
    To copy to clipboard, switch view to plain text mode 


    Please see the attached code and if anyone is able to help, that would be very much appreciated.
    If more info is needed, please ask.

    //main.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3.  
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. qDebug()<<"From main GUI thread: "<<QThread::currentThreadId();
    9.  
    10.  
    11. //Create GUI, but it is not shown until GUI.show is called
    12. MainWindow GUI;
    13. //generate GUI screen when this main function is finished
    14. GUI.show();
    15.  
    16. return a.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 


    //mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QtCore>
    6.  
    7.  
    8. namespace Ui {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit MainWindow(QWidget *parent = 0); //Class constructor
    18.  
    19. private:
    20. Ui::MainWindow *ui;
    21.  
    22. signals:
    23. void runInitializeInthreadUDP();
    24.  
    25. private slots:
    26. void on_pushButton_clicked();
    27. };
    28.  
    29. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 


    //mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QtCore>
    4. #include "myudp.h"
    5. #include "myThread.h"
    6.  
    7.  
    8. myThread* threadObj = new myThread();
    9. Worker* worker = new Worker();
    10.  
    11.  
    12. //MainWindow Class Constructor
    13. MainWindow::MainWindow(QWidget *parent) :
    14. QMainWindow(parent),
    15. ui(new Ui::MainWindow)
    16. {
    17. ui->setupUi(this);
    18.  
    19. //Create UDP object and startup worker thread
    20. //https://wiki.qt.io/QThreads_general_usage
    21. MyUDP udpObj;
    22. udpObj.moveToThread(threadObj);
    23. connect(worker, SIGNAL (&Worker::threadFinishedSignal), threadObj, SLOT (quit()));
    24. connect(worker, SIGNAL (&Worker::threadFinishedSignal), worker, SLOT (deleteLater()));
    25. connect(threadObj, SIGNAL (&Worker::threadFinishedSignal), threadObj, SLOT (deleteLater()));
    26. threadObj->start();
    27.  
    28.  
    29. //
    30. //
    31. //THIS NEXT PART DOES NOT WORK(Initialize() SLOT() function is never called)!!!!
    32. //
    33. //
    34. //initialize UDP ports
    35. //QT5 VERSION:
    36. connect( this, SIGNAL(&MainWindow::runInitializeInthreadUDP), threadObj, SLOT(&MyUDP::Initialize), Qt::QueuedConnection);
    37. //QT4 VERSION:
    38. // connect( this, SIGNAL(runInitializeInthreadUDP()), threadObj, SLOT(Initialize()), Qt::QueuedConnection);
    39. emit runInitializeInthreadUDP();
    40. }
    41.  
    42.  
    43. //also does not work when pressing button on GUI (this _clicked function runs, but does not call SLOT &MyUDP::Initialize)
    44. void MainWindow::on_pushButton_clicked()
    45. {
    46. emit runInitializeInthreadUDP();
    47. }
    To copy to clipboard, switch view to plain text mode 

    //mythread.h
    Qt Code:
    1. //#ifndef WORKERTHREAD_H
    2. //#define WORKERTHREAD_H
    3.  
    4. #include <QtCore>
    5.  
    6.  
    7. //http://blog.debao.me/2013/08/how-to-use-qthread-in-the-right-way-part-1/
    8. class myThread : public QThread
    9. {
    10. Q_OBJECT
    11.  
    12. private:
    13. void run()
    14. {
    15. qDebug()<<"From worker thread: "<<currentThreadId();
    16. connect(this, &QThread::finished, this, &QObject::deleteLater);
    17.  
    18. exec();
    19. }
    20. };
    21.  
    22.  
    23.  
    24. //https://wiki.qt.io/QThreads_general_usage
    25. class Worker : public QObject {
    26. Q_OBJECT
    27.  
    28. signals:
    29. void threadFinishedSignal();
    30. };
    31.  
    32. //#endif // WORKERTHREAD_H
    To copy to clipboard, switch view to plain text mode 

    //myudp.h
    Qt Code:
    1. #ifndef UDP_H
    2. #define UDP_H
    3.  
    4. //Base class QObject
    5. #include <QUdpSocket>
    6. #include <QtCore>
    7.  
    8.  
    9. class MyUDP : public QObject
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. MyUDP(); //Constructor for UDP Class Object
    15. ~MyUDP(); //Destructor for UDP Class Object
    16.  
    17. private:
    18. QUdpSocket *socket;
    19.  
    20. public slots:
    21. void UDPreadInputData();
    22. //Setup UDP port and connect port to fire function each time a UDP packet is received
    23. void Initialize();
    24. };
    25.  
    26.  
    27. #endif // UDP_H
    To copy to clipboard, switch view to plain text mode 


    //myudp.cpp
    Qt Code:
    1. #include "myudp.h"
    2. #include <QtCore>
    3.  
    4.  
    5. //Settings for UDP addresses
    6. QHostAddress remoteAddress = QHostAddress("192.168.1.1");
    7. QHostAddress localAddress = QHostAddress("192.168.1.2");
    8. quint16 sendPort = 1234;
    9. quint16 receivePort = 5678;
    10.  
    11. //UDP buffer that will contain the data received from microcontroller
    12. QByteArray Buffer;
    13.  
    14.  
    15.  
    16. MyUDP::MyUDP() //Constructor for UDP Class Object
    17. {
    18. }
    19. MyUDP::~MyUDP() //Destructor for UDP Class Object
    20. {
    21. }
    22.  
    23.  
    24.  
    25. void MyUDP::Initialize()
    26. {
    27. socket = new QUdpSocket(this);
    28.  
    29. //We need to bind the UDP socket to an address and a port
    30. // socket->bind(QHostAddress::LocalHost,receivePort);
    31. socket->bind(localAddress,receivePort);
    32. //Call the "UDPreadyRead" function each time the socket gets data on the defined UDP port
    33. connect(socket,SIGNAL(readyRead()),this,SLOT(UDPreadInputData()));
    34.  
    35. qDebug()<<"UDP worker thread initialized";
    36. }
    37.  
    38.  
    39.  
    40. //Read something
    41. void MyUDP::UDPreadInputData()
    42. {
    43. Buffer.resize(socket->pendingDatagramSize());
    44. int bufferSize = Buffer.size();
    45.  
    46. //read sing UDP packet from hardware ethernet tranceiver on computer
    47. socket->readDatagram(Buffer.data(), bufferSize, &remoteAddress, &receivePort);
    48.  
    49. // http://lists.qt-project.org/pipermail/qt-interest-old/2009-June/008318.html
    50. // Read float32 data types out with a QDataStream
    51. QDataStream ds( Buffer.mid( 0, bufferSize ) );
    52. //By default, a QDataStream is big endian and must be flipped depending on "processor architecture".
    53. ds.setByteOrder(QDataStream::LittleEndian);
    54. ds.setFloatingPointPrecision(QDataStream::SinglePrecision);
    55.  
    56. //Throw out UDP Ethernet header and two bytes that are unused at beginning of packet (10 bytes)
    57. for(int i; i<5; i++)
    58. {
    59. qint16 temp; //each int16 is 2 bytes
    60. ds >> temp;
    61. }
    62.  
    63. //Read the first two "start of sample bytes"
    64. qint16 HeaderByte1;
    65. ds >> HeaderByte1; //54321 start of sample int16
    66. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: MainWindow GUI cannot call SLOT in worker thread UDP object

    connect( this, SIGNAL(&MainWindow::runInitializeInthreadUDP), threadObj, SLOT(&MyUDP::Initialize), Qt::QueuedConnection);
    You are mixing up the two different styles for the connect() statement. Your code -should- read:

    Qt Code:
    1. connect( this, &MainWindow::runInitializeInthreadUDP, threadObj, &MyUDP::Initialize, Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    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: MainWindow GUI cannot call SLOT in worker thread UDP object

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QtCore>
    4. #include "myudp.h"
    5. #include "myThread.h"
    6.  
    7.  
    8. myThread* threadObj = new myThread();
    9. Worker* worker = new Worker();
    10.  
    11.  
    12. //MainWindow Class Constructor
    13. MainWindow::MainWindow(QWidget *parent) :
    14. QMainWindow(parent),
    To copy to clipboard, switch view to plain text mode 

    That really hurts my eyes and brains.
    This is C++ not C and even in C you use globals only if really really needed.
    ==========================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. Replies: 1
    Last Post: 26th November 2016, 20:01
  2. Replies: 4
    Last Post: 26th June 2014, 19:27
  3. Replies: 9
    Last Post: 19th May 2014, 08:20
  4. Call method of Object in different Thread
    By Qiieha in forum Qt Programming
    Replies: 3
    Last Post: 26th July 2012, 14:14
  5. slot in worker thread is not called
    By phenoboy in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2012, 20:45

Tags for this Thread

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.