1
votes

I'm using the function setMouseCallback to extract the information about pixel coordinates at every mouse event. The program I created works perfectly if I use openCV windows. Precisely:

image is cv::Mat;

cv::namedWindow("Original", WINDOW_NORMAL);
cv::imshow("Original", image);

cv::setMouseCallback("Original", mouseWrapper, NULL);

where

void esempio::onMouse(int event, int x, int y, int flags, void *param)
{
//---------------------------------------------------------
// Code to read the mouse event in the identification of a point
//---------------------------------------------------------
    if (event == CV_EVENT_LBUTTONDOWN)
        {
            std::cout << "1: " << x << "," << y << std::endl;
            pp_m.x=x;
            pp_m.y=y;
        }
}

void mouseWrapper( int event, int x, int y, int flags, void* param )
{
    esempio * mainWin = (esempio *)(param);
    mainWin->onMouse(event,x,y,flags,0);
}

Now, I would like to use the same code in a QLabel created in my interface. I tried to use the function setWindowTitle to change the name of the QLabel in this way:

ui->label_show->setWindowTitle("Test");
cv::setMouseCallback("Test", mouseWrapper, NULL); 

but this approach seems not adequate.

How can I indicate to the function setMouseCallback to work on the desired QLabel?

Thanks

1

1 Answers

0
votes

This can be hard because:

  • I'm not sure that the OS is taking into account titles of child widgets, and I'm not sure that OpenCV will recognize window title of a non-top-level widget;
  • Qt handles child widgets internally and doesn't expose them to OS unless forced to do that;
  • Most importantly, OpenCV won't have a chance to call your callback because Qt manages the event loop.

I don't see why not use Qt's own means to react on mouse events.

Call ui->label_show->installEventFilter(this) in constructor of your form and implement virtual eventFilter function. In this function you can use event argument to retrieve event type and mouse coordinates (after casting to QMouseEvent).

See event filters.