[SOLVED] Mouse Tracking With GUI Builder



  • Hi, My goal is to create a simple test application where a dial will be moved by the movement of a mouse.
    This is to say ONLY the movement of the mouse. I was trying something like

    @MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    this->setMouseTracking(true);

    }@

    @void MainWindow::mouseMoveEvent(QMouseEvent* )
    {
    ui->dial->setValue(4);
    }@

    However it didn't seem to work. It only worked when the mouse was pressed which was not my goal.

    Thank you for your help



  • You can use an event filter on the application.
    Define and implement bool MainWindow::eventFilter(QObject*, QEvent*). For example..

    @bool MainWindow::eventFilter(QObject *obj, QEvent *event)
    {
    if (event->type() == QEvent::MouseMove)
    {
    QMouseEvent mouseEvent = static_cast<QMouseEvent>(event);
    statusBar()->showMessage(QString("Mouse move (%1,%2)").arg(mouseEvent->pos().x()).arg(mouseEvent->pos().y()));
    }
    return false;
    }
    @

    Install the event filter when the MainWindows is constructed (or somewhere else). For example..

    @MainWindow::MainWindow(...)
    {
    ...
    qApp->installEventFilter(this);
    ...
    }
    @



  • Thanks! that worked great.



  • please mark ur thread as solved...



  • Hai mrstarware,

    Can you please post your complete code of mouse movements because my requirement is similar one i.e to display co-ordinates on world map with movement of mouse on map.



  • Hai shoyeb,

    Can you help me in my requirement of displaying world map and co-ordinates on map with mouse movements


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.