gpt4 book ai didi

C++ QT 和 OpenCV。 QLabel 中的 setMouseCallBack?

转载 作者:太空宇宙 更新时间:2023-11-04 13:35:21 25 4
gpt4 key购买 nike

我正在使用函数 setMouseCallback 来提取有关每个鼠标事件的像素坐标的信息。如果我使用 openCV 窗口,我创建的程序可以完美运行。准确地说:

图像是cv::Mat;

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

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

在哪里

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);
}

现在,我想在我的界面中创建的 QLabel 中使用相同的代码。我尝试使用函数 setWindowTitle 以这种方式更改 QLabel 的名称:

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

但这种方法似乎还不够。

我如何指示函数 setMouseCallback 处理所需的 QLabel?

谢谢

最佳答案

这可能很难,因为:

  • 我不确定操作系统是否考虑了子窗口小部件的标题,我也不确定 OpenCV 是否会识别非顶级窗口小部件的窗口标题;
  • Qt 在内部处理子部件,除非被迫这样做,否则不会将它们暴露给操作系统;
  • 最重要的是,OpenCV 没有机会调用您的回调,因为 Qt 管理事件循环。

我不明白为什么不使用 Qt 自己的方法来对鼠标事件使用react。

在表单的构造函数中调用 ui->label_show->installEventFilter(this) 并实现虚拟 eventFilter 函数。在此函数中,您可以使用 event 参数来检索事件类型和鼠标坐标(在转换为 QMouseEvent 之后)。

参见 event filters .

关于C++ QT 和 OpenCV。 QLabel 中的 setMouseCallBack?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29751139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com