QT passing data between threads, worker function doWork() doesn't start

49 Views Asked by At

I'm new in Qt and I have a problem. I'm trying send data from worker thread using signal dataReady to slot in mainwindow updateData. My problem is that my doWork function doesn't starts and don't emit signal. The project is being develop on RPi 4B in Qt5. What I have done wrong, because I tried many examples from internet and nothing is changing.

worker header file

class MyThreadWorker : public QThread
{
    Q_OBJECT

public:
    float x;
    float y;
    float z;
    int correct;

public:
      MyThreadWorker();

private:
    Adafruit_BNO055 * imu_first;
    QTimer * timer;

signals:
    void dataReady(float x, float y, float z, int correct);

public slots:
    void doWork();
    void start();
};

worker source file

void MyThreadWorker::start()
{
    imu_first = new Adafruit_BNO055;
    imu_first->imu_initialization();

    timer = new QTimer();
    timer->start(1000);
    qDebug() << QString("start work in time");
    connect(timer,SIGNAL(timeout()),this,SLOT(doWork()));
}

void MyThreadWorker::doWork()
{
    imu_first->sig_bno_handler();
    gpioSleep(0, 0, 1000);
    imu_first->savevector();
    //zapis danych
    x=imu_first->myStruct.x;
    y=imu_first->myStruct.y;
    z=imu_first->myStruct.z;
    correct=imu_first->init_correct;
    qDebug()<<"Dane: " << x;

    emit dataReady(x, y, z, correct);
}

and mainwindow source code

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //imuobiekt = new Adafruit_BNO055();
    auto thread = new QThread;
    auto worker = new MyThreadWorker();
     
    worker->moveToThread(thread);

    connect(thread, SIGNAL(started()), worker , SLOT(start()));
    connect(worker, SIGNAL(dataReady(float,float,float,int)), this, SLOT(updateData(float,float,float,int)));

    worker->start();

    //setting the temperature sliders range
    ui->slider1->setMinimum(10);
    ui->slider2->setMinimum(10);
    ui->slider1->setMaximum(50);
    ui->slider2->setMaximum(50);

    //imuobiekt->imu_initialization();
    gpioSleep(0, 0, 2000);
}

void MainWindow::updateData(float x, float y, float z, int correct)
{
  //procces data in mainwindow
}

I really want to make it work.

0

There are 0 best solutions below