Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How can i automatize mouse event?



  • Hi everyone. I have an artificial horizon. This is my mainwindow.cpp
    I want to add timer for mouse event, for example, it should turn to the right in every 20 sec. How can i do it?

    #include <cmath>
    #include <QThread>
    #include <QMouseEvent>
    #include <QDebug>
    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "artificialhorizon.h"
    
    #define TIMEOUT (500)
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow),
        state(0),
        roll(0),
        pitch(0)
    {
        ui->setupUi(this);
        this->h = new ArtificialHorizon(this->ui->centralWidget);
        h->show();
    
        connect(&(this->timer), SIGNAL(timeout()), this, SLOT(timedOut()));
        timer.setInterval(TIMEOUT);
        timer.start();
    
    
    //    connect(this, SIGNAL(timeout()), this, SLOT(mouseMoveEvent()));
    //    timer.start(200);
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::timedOut()
    {
        switch(state)
        {
        case 0:
            pitch += 5;
            break;
        case 1:
            pitch -= 5;
            break;
        case 2:
            pitch += 10;
            break;
        default:
            timer.stop();
            break;
        }
    
        if(state==0 && pitch>90)  //Eğer state 0sa ve pitch >90ise
        {
            state++;
            roll = 180-roll;
            pitch = 180 - pitch;
        }
        else if(state==1 && pitch < -90)
        {
            roll = 180-roll;
            pitch = -180-pitch;
            state++;
        }
        else if(state==2 && pitch > 10)
        {
            state++;
        }
    
        qDebug() << roll << "," << pitch;
        h->setRollPitch(roll,pitch);
    }
    
    void MainWindow::mouseMoveEvent(QMouseEvent *event)
    {
        int pitch = event->pos().y();
        int roll = event->pos().x();
    
        if(pitch>90)
        {
            roll = 180-roll;
            pitch = -180+pitch;
        }
        else if(pitch<-90)
        {
            roll = 180-roll;
            pitch = 180+pitch;
        }
    
        // qDebug() << event->pos().x() << "," << event->pos().y() <<  " : " << roll << "," << pitch;
    
        this->h->setRollPitch(roll, pitch);
        QMainWindow::mouseMoveEvent(event);
    }
    

  • Moderators

    Perhaps you can use mouseMove() from QTest?

    Also, you can simply manually construct a QMouseEvent using new.



  • @sierdzio Thank you, i will try it


Log in to reply