2

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.

Petersaber
  • 851
  • 1
  • 12
  • 29

1 Answers1

2

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.

Nejat
  • 31,784
  • 12
  • 106
  • 138
  • Great! Not only this works, but also the coordinates are not relative to the whole window, just the label, which I didn't realise would happen. Thanks for this answer. – Petersaber May 27 '15 at 06:18