简体   繁体   English

c ++ QT和OpenCV。 QLabel中的setMouseCallBack吗?

[英]c++ QT and OpenCV. setMouseCallBack in QLabel?

I'm using the function setMouseCallback to extract the information about pixel coordinates at every mouse event. 我正在使用功能setMouseCallback来提取有关每个鼠标事件的像素坐标的信息。 The program I created works perfectly if I use openCV windows. 如果使用openCV窗口,则创建的程序可以完美运行。 Precisely: 正是:

image is cv::Mat; 图片是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. 现在,我想在界面中创建的QLabel中使用相同的代码。 I tried to use the function setWindowTitle to change the name of the QLabel in this way: 我试图通过以下方式使用函数setWindowTitle更改QLabel的名称:

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? 如何指示函数setMouseCallback在所需的QLabel上工作?

Thanks 谢谢

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; 我不确定操作系统是否考虑了子窗口小部件的标题,也不确定OpenCV是否可以识别非顶级窗口小部件的窗口标题。
  • Qt handles child widgets internally and doesn't expose them to OS unless forced to do that; Qt在内部处理子窗口小部件,除非被迫这样做,否则它们不会暴露给操作系统。
  • Most importantly, OpenCV won't have a chance to call your callback because Qt manages the event loop. 最重要的是,OpenCV将没有机会调用您的回调,因为Qt管理事件循环。

I don't see why not use Qt's own means to react on mouse events. 我不明白为什么不使用Qt自己的方式对鼠标事件做出反应。

Call ui->label_show->installEventFilter(this) in constructor of your form and implement virtual eventFilter function. 在表单的构造函数中调用ui->label_show->installEventFilter(this)并实现虚拟eventFilter函数。 In this function you can use event argument to retrieve event type and mouse coordinates (after casting to QMouseEvent ). 在此函数中,您可以使用event参数来检索事件类型和鼠标坐标(在强制转换为QMouseEvent )。

See event filters . 请参阅事件过滤器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM