問題描述
我有一個包含多個孩子的 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 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()
而不是 contentsRect()
這個特例.
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 */
}
最好不要將矩形映射到全局,而是將全局光標位置映射到小部件:
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 - 確定絕對小部件和光標位置的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!