Qt - 确定绝对小部件和光标位置 [英] Qt - Determine absolute widget and cursor position

查看:262
本文介绍了Qt - 确定绝对小部件和光标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个QWidget包含多个孩子。最终的目标是能够从一个部件拖放到另一个部件,在部件之间移动。我有一个信号被触发到我的父控件的控制器,并可以确定拖动开始和结束正确。我当前的问题是确定鼠标是否在鼠标上方的目标小部件上。

I have a QWidget that contains multiple children. The ultimate goal is to be able to drag and drop from one widget to the other, moving something between widgets. I've got a signal being fired to my parent widget's controller and can determine when the drag is starting and ending correctly. My current problem is determining whether or not the mouse is above the target widget on mouse up.

当我看到 underMouse 在文档中,但它不工作在拖放事件(当我测试,似乎返回不正确的值)。没有这个,我的目标是找到包含目标小部件的矩形,并找到它是否包含鼠标向上的鼠标坐标。我不能简单地使用 contentsRect ,因为它返回相对于被调用的窗口小部件的位置。我认为 mapToGlobal 会给我绝对的屏幕像素值,但它仍然失败。我尝试在父窗口部件的窗口上调用mapTo,但是这也似乎失败了。

I got excited when I saw underMouse in the docs, but it doesn't work during drag and drop events (when I tested, it seemed to return incorrect values). Failing this, my goal was to find the rectangle containing the target widget and find whether it contained the mouse coordinates on a mouse up. I can't simply use contentsRect, since it returns positions relative to the widget it is being called on. I thought that mapToGlobal would give me absolute screen pixel values, but it keeps failing as well. I tried calling mapTo on the parent widget's window, but that also seemed to fail.

下面是显示各种方法的各种QRect和QPoint的代码。

Below is code showing the various QRects and QPoints I've gotten with the various methods. Maybe there's a simple error with one of them, so I've provided them all.

QRect relativeWidgetRect = targetWidget->contentsRect();
QRect *absoluteWidgetRect = new QRect(QWidget::mapToGlobal(relativeWidgetRect.topLeft()), QWidget::mapToGlobal(relativeWidgetRect.bottomRight()));
QRect *widgetRect = new QRect(mapTo(window(), relativeWidgetRect.topLeft()), mapTo(window(), relativeWidgetRect.bottomRight()));
QPoint relativeMousePos = QCursor::pos();
QPoint absoluteMousePos = QWidget::mapToGlobal(relativeMousePos);
QPoint widgetMousePos = mapTo(window(), relativeMousePos);

mapToParent 不会为我的目的工作,因为目标窗口小部件实际上是由顶层父级的子级父级。

mapToParent won't work for my purposes, since the target widget is actually parented by a child of the top-level parent.

Update 这里是最后编写的代码。在我的顶级窗口小部件(这是源和目标窗口小部件的祖先)中,我添加了:

Update Here's the code that ended up working out. In my top-level widget (that was an ancestor to both the source and target widgets), I added this:

QRect widgetRect = targetWidget->Geometry();
QPoint mousePos = targetWidget->mapFromGlobal(QCursor::pos());
if(widgetRect.contains(mousePos))
{
// Logic
}


推荐答案

如前所述,你应该使用 targetWidget-> geometry() $ c> contentRect()在这个特殊情况下。

As already said you should rather use targetWidget->geometry() instead of contentsRect() in this special case.

接下来我不知道你发布的代码属于哪个类。方法 QWidget :: mapToGlobal()应该从QWidget调用你的坐标是相对的。如果我有你的权利,它应该看起来像这样:

Next I wonder which class the code you posted belongs to. The method QWidget::mapToGlobal() should be invoked from the QWidget your coordinates are relative to. If I got you right, it should look like something like this:

QRect widgetRect = targetWidget->geometry();
widgetRect.moveTopLeft(targetWidget->parentWidget()->mapToGlobal(widgetRect.topLeft()));

然后注意 QCursor :: pos()已经返回全局屏幕坐标,因此无需在这里映射任何内容:

Then note QCursor::pos() is already returning global screen coordinates, so no need to map anything here:

if (widgetRect.contains(QCursor::pos())) {
    /* swap widgets */
}

编辑:可能更好不将rect映射到全局,而是将全局光标位置映射到窗口小部件:

Probably it's even better to not map the rect to global, but to map the global cursor position to the widget:

if (targetWidget->rect().contains(targetWidget->mapFromGlobal(QCursor::pos()))) {
    /* do stuff */
}

这篇关于Qt - 确定绝对小部件和光标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆