Petersaber
Petersaber

Reputation: 893

Qt - mouse events on a QLabel

I've got this test piece of code in mainwindow.cpp:

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

I just want to get mouse position when clicked, and send the coordinates to another thread with an OpenCV loop that'll pull HSV information and do things accordingly. I'm using mouse over just for testing.

The problem is that I have no idea how to attach this (tracking, clicking) to a QLabel labelKalibracja, one I use to display video frames, instead of the whole window.

ui->labelKalibracja->installEventFilter(this);

is supposed to work, but doesn't, but

qApp->->installEventFilter(this);

Will make the whole window a mouse track zone.

Upvotes: 2

Views: 4814

Answers (1)

Nejat
Nejat

Reputation: 32685

You should check the object of the event filter :

if (qobject_cast<QLabel*>(obj)==ui->labelKalibracja && event->type() == QEvent::MouseMove)
{
   ...
}

Now you can make sure that the event is for the label. Note that the event filter could be installed on multiple objects and it's your duty to identify the combination of objects and events.

Upvotes: 2

Related Questions