qt窗口左下角在鼠标位置弹出

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了qt窗口左下角在鼠标位置弹出相关的知识,希望对你有一定的参考价值。

1、只要确定弹窗左上角的合理位置就可以了。

2、合理位置: 简单的一种就是保证其必在主窗口内。思路就是,判断弹窗左上角和右下角的坐标值是否超过主窗口的边界值来重新设置弹窗的左上角的坐标值。

三、代码实现:

1 // mouseGPos : 当前鼠标的绝对坐标
2 // pMainWgt : 主窗口
3 // pPopWgt : 弹窗
4 QPoint CToolBarWgt::getFixGlobalPos(QPoint mouseGPos, QWidget* pMainWgt, QWidget* pPopWgt)
5
6 QPoint mianGPoint = pMainWgt->mapToGlobal(pMainWgt->pos());
7 int nMainLeft = mianGPoint.x();
8 int nMainTop = mianGPoint.y();
9 int nMainRight = mianGPoint.x()+ pMainWgt->width();
10 int nMainBottom = mianGPoint.y() + pMainWgt->height();
11
12 int nPopLeft = mouseGPos.x() - pPopWgt->width()/2 ;
13 int nPopTop = mouseGPos.y() - pPopWgt->height();
14 int nPopRight = mouseGPos.x() + pPopWgt->width()/2;
15 int nPopBottom = mouseGPos.y();
16
17 // 最终要确定的只有左上角的坐标位置:
18 nPopLeft = nPopLeft < nMainLeft ? nMainLeft: nPopLeft; // 弹窗左边超出主窗口,赋值为主窗口边界值。
19 nPopTop = nPopTop < nMainTop ? nMainTop : nPopTop; //弹窗上部分超出主窗口上部值,赋值为主窗口上半边界值。
20 nPopLeft = nPopRight > nMainRight ? nPopLeft - (nPopRight - nMainRight) : nPopLeft; //弹窗右侧超出主窗口右侧边界值,则弹窗左侧值向左移动超出的部分
21 nPopTop = nPopBottom > nMainBottom ? nPopTop - (nPopBottom - nMainBottom): nPopTop; //弹窗下侧超出主窗口下侧边界值,则弹窗上侧值向上移动超出的部分
22 // 返回的QPoint是绝对坐标。
23 return QPoint(nPopLeft, nPopTop);
24
参考技术A qt窗口左下角在鼠标位置弹出可以用以下方法解决
在构造函数里面,根据qt的帮助文档,mousemoveEvent事件如果mouserTrack为false,那么只有鼠标按下才会执行,而且默认是false,所以需要把窗口事件设置一下
然后就简单了

以上是关于qt窗口左下角在鼠标位置弹出的主要内容,如果未能解决你的问题,请参考以下文章

如何获得 Qt窗口部件在主窗口中的位置--确定鼠标是否在某一控件上与在控件上的位置

如何在鼠标单击位置打开弹出框

Qt设置窗口可拖动

Qt 如何获取滚动窗体中鼠标点击的坐标

C#winform有2个panel,在右边panel获取鼠标的坐标(x,y),如何传递到panel1上弹出的窗口form2里面呢

word滚动鼠标滑轮会放大缩小怎么办