700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > [Qt5] 鼠标中心为基准缩放图像(halcon实现)

[Qt5] 鼠标中心为基准缩放图像(halcon实现)

时间:2020-10-09 22:30:28

相关推荐

[Qt5] 鼠标中心为基准缩放图像(halcon实现)

📢博客主页:https://loewen.📢欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!📢本文由 丶布布原创,首发于 CSDN,转载注明出处🙉📢现在的付出,都会是一种沉淀,只为让你成为更好的人✨

文章预览:

一. 图像缩放的实现二. 捕获窗体QWidget大小的改变,更新图像显示三. 实现效果展示

一. 图像缩放的实现

本文通过继承窗体类QWidget的鼠标滑轮事件void wheelEvent(QWheelEvent *event)来实现以鼠标位置为基点对图像进行缩放。此外,还可以通过过滤器事件(里面可以放滚动事件,左击事件、右击事件等)来实现图像缩放,感兴趣的自行搜索。

核心代码:

void FormScalingTest::wheelEvent(QWheelEvent *event){bool isScale = true; //是否支持缩放if (isScale){int num_notch = std::abs(event->delta()) / 120;double factor = (event->delta() > 0) ? std::sqrt(2.0) : 1.0 / std::sqrt(2.0);while (num_notch > 1){factor = factor * ((event->delta() > 0) ? std::sqrt(2.0) : 1.0 / std::sqrt(2.0));num_notch--;}HTuple centerRow, centerCol, hvButton;double row1, col1, row2, col2;try{GetMposition(m_windowHandle, &centerRow, &centerCol, &hvButton);GetPartFloat(&row1, &col1, &row2, &col2);}catch (HException& e){return;}double left = centerRow - row1;double right = row2 - centerRow;double top = centerCol - col1;double bottom = col2 - centerCol;double newRow1 = centerRow - left * factor;double newRow2 = centerRow + right * factor;double newCol1 = centerCol - top * factor;double newCol2 = centerCol + bottom * factor;try{SetPartFloat(newRow1, newCol1, newRow2, newCol2);SetSystem("flush_graphic", "false");ClearWindow(m_windowHandle);SetSystem("flush_graphic", "true");this->repaint();}catch (HOperatorException){return;}}}

知识拓展:过滤器方式触发实现滑轮缩放事件

事件过滤器可以接收一个对象的所有事件,当这个对象收到事件之前,事件过滤器通过eventFilter()函数先接收事件。eventFilter()函数返回true,则取消事件(事件不再向目标对象发送);返回false,则事件被继续发往目标对象。事件过滤器可以是任何从QObject继承的对象,只要实现eventFilter()这个函数。

① 为需要响应事件的控件安装事件过滤(初始化):

ui.label->setMouseTracking(true);ui.label->installEventFilter(this);

② 过滤器事件(触发滑轮事件)

bool FormScalingTest::eventFilter(QObject *obj, QEvent *event){if (obj == ui.ImageWidget) //作用区域{if (event->type() == QEvent::Wheel){QWheelEvent *WheelEvent = static_cast<QWheelEvent*>(event);this->LableMouseWheel(WheelEvent);}}return false;}

③ 滑轮实现函数

void FormScalingTest::LableMouseWheel(QWheelEvent *event){//上面的wheelEvent(QWheelEvent *event)函数功能}

二. 捕获窗体QWidget大小的改变,更新图像显示

本文通过继承窗体类QWidget的窗体改变事件void resizeEvent(QResizeEvent *event)来捕获窗体改变时间,并实时更新图像显示大小。

核心函数:

void FormScalingTest::resizeEvent(QResizeEvent * _event){Q_UNUSED(_event);CloseWindow(m_windowHandle);m_winID = (Hlong)ui.ImageWidget->winId();SetWindowAttr("background_color", "black"); //设置窗口颜色OpenWindow(0, 0, ui.ImageWidget->width(),ui.ImageWidget->height(),m_winID, "visible", "", &m_windowHandle);if (!m_imageWidth.Length() > 0) {return;}else{dispImage(m_image,ui.ImageWidget);}}

三. 实现效果展示

工程代码链接:/download/weixin_43197380/85299967

其他相似功能:[Qt5] 右键窗体弹出菜单,实现图像适应窗体大小(纯Qt实现)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。