Unsolved Threads and SIGNAL SLOT Method
-
Hi Guys :)
i need some Help with SIGNAL and SLOT..
i created a mainWindow ui ... and also a thread which collect DATA from UDP ...
My Problem now is that i want to show this DATA in the UI ...
So i have done some research and many suggest the SIGNAL and SLOT Method ...
i created it like in several Tutorial but it doesn't work ... The Signal doesn't emit ... so it doesnt run the SLOT Method in the Mainwindow ...Here is my COde :
I hope that sb can help me ..
MainWindow cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QtCore> #include <mythread.h> #include <QThread> #include <QtWidgets> #include <QtNetwork> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); recieve = new recievesesnsordata(this); connect(recieve,SIGNAL(senddata()),this,SLOT(WriteImuData())); return; } } MainWindow::~MainWindow() { delete ui; } void MainWindow::WriteImuData() { qDebug() << "Test"; }
Mainwindow .h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <QTimer> #include <qcustomplot.h> #include "recievesesnsordata.h" namespace Ui { class MainWindow; } class QUdpSocket; class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); QThread *thread; recievesesnsordata *recieve; public slots: void on_btnPauseResume_clicked(); void on_startBroadcasting_clicked(); void on_pushButton_clicked(); void WriteImuData(); public: Ui::MainWindow *ui; //__________________________________________________________ }; #endif // MAINWINDOW_H
recieve Sensor DATA .h
#ifndef RECIEVESESNSORDATA_H #define RECIEVESESNSORDATA_H #include <QObject> #include <QDebug> #include <QThread> #include <QTimer> class QUdpSocket; class recievesesnsordata : public QObject { Q_OBJECT public: explicit recievesesnsordata(QObject *parent = nullptr); void DoSetup(QThread &cThread); void run(); QTimer *tmrTimer; QThread *thread; public slots: void DoWork(); void readyRead(); private: QUdpSocket *socket; QUdpSocket *udpSocket2; QUdpSocket *UdpSocket; signals: void senddata(); }; #endif // RECIEVESESNSORDATA_H
recievesensor.cpp
#include "recievesesnsordata.h" #include <QtNetwork> #include "mainwindow.h" #include "ui_mainwindow.h" recievesesnsordata::recievesesnsordata(QObject *parent) : QObject(parent) { udpSocket2 = new QUdpSocket(); udpSocket2->bind(45455,QUdpSocket::ShareAddress); thread = new QThread(); } void recievesesnsordata::DoSetup(QThread &cThread) { connect(&cThread, SIGNAL(started()),this,SLOT(DoWork())); // connect(this, SIGNAL(enableSensorWrite()),MainWindow,SLOT(WriteImuData())); } void recievesesnsordata::DoWork() { QList<QByteArray> test; for(int i = 0; i<1000;i++){ /*QByteArray datagram; datagram.resize(udpSocket2->pendingDatagramSize()); udpSocket2->readDatagram(datagram.data(), datagram.size()); test.push_back(datagram.data());*/ //qDebug() << test; qDebug() << i; thread->msleep(500); emit senddata(); } }
main.cpp
#include "mainwindow.h" #include <QApplication> #include "mythread.h" #include <QtNetwork> #include <QtSql> #include <QThread> #include "recievesesnsordata.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; QThread cThread; recievesesnsordata cObject; cObject.DoSetup(cThread); cObject.moveToThread(&cThread); cThread.start(); w.show(); return a.exec(); }
-
@BadHombre Do not use a loop and a sleep! Else the signal will not be emitted as long as you're inside the loop. A loop blocks the event loop in your thread - with blocked event loop signals are blocked!
Also you do NOT execute the data handling in a thread. You only create a QThread instance, but I don't see where you actually start the thread? And what is this thread supposed to do? Take a look at https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/ -
@jsulm
Actually he callscThread.start();
in main. Linethread = new QThread()
is pretty useless though.@BadHombre
You should communicate with socket in asynchronous way withreadyRead()
signal. Look at example from official documentation. -
This post is deleted! -
The Problem is that i get all the TIme DATA ... and that's why i want to use a thread :/ i deleted also the Loop but its still don't emit the the signal or run the SLOT Function
-
@BadHombre
Regarding your signal/slot problem:you create a
recievesesnsordata
object and connection in MainWindow constructorrecieve = new recievesesnsordata(this); connect(recieve,SIGNAL(senddata()),this,SLOT(WriteImuData()))
But in main function you create a new object, then work with it.
recievesesnsordata cObject;