QPoint 全局/局部映射

发布于 2024-10-06 18:11:51 字数 319 浏览 0 评论 0原文

我正在尝试更改 QPushButton 子类的尺寸,同时锚定用户定义的角(并不总是左上角)。

QPushButton 将 QFrame 定义为其父级,并且在窗口中自由浮动。当我尝试在子类代码中更改 QPushButton 子类的大小时,我相信这给我带来了麻烦,因为 mouseEvent 提供的 QPoint 是本地的。

我尝试使用 myButton->mapToGlobal(QPoint)myButton->mapFromGlobal,但我认为我不太明白它们是如何工作的。寻找例子让我两手空空。

I am trying to change the dimensions of a QPushButton subclass while anchoring a user-defined corner (not always the top left corner).

The QPushButton defines a QFrame as its parent, and is free-floating in the window. When I try to change the size of the QPushButton subclass in the subclass code, I believe it is giving me trouble since the QPoints provided by mouseEvents are local.

I have tried to work with the myButton->mapToGlobal(QPoint) and myButton->mapFromGlobal, but I don't think I quite understand how they work. A search for examples has left me empty-handed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睫毛上残留的泪 2024-10-13 18:11:51

本地(小部件)坐标相对于小部件的左上角。全局坐标是屏幕坐标。它们很容易转换,并且像 QMouseEvent 这样的事件提供本地 (pos()) 和全局 (globalPos()) 坐标。
如果你想从 Widget A 映射到 Widget B,你可以这样做

const QPoint global = a->mapToGlobal( localPosInA );
const QPoint localInB = b->mapFromGlobal( global );

或者,更短:

const QPoint localInB = a->mapTo( b, localPosInA );

示例:假设你有一个位于 (100,110)(屏幕坐标)的顶级小部件 w1,它有一个位于 (10,10,110) 的子小部件 w2 10) (w1 坐标) 和 w2 中 (20, 20) (w2 坐标) 处有鼠标事件,则鼠标光标的全局位置为

(100,110) + (10,10) + (20,20) = (130,140 ) (屏幕坐标)

即 w2->mapToGlobal( mousePos )。

w2->mapTo( w1, mousePos ) 或者,由于 w1 是 w2 的父级,因此 w2->mapToParent( mousePos ) 是

(10,10) + (20,20) = (30,30) (w1 坐标)。

如果您将所有内容都转换为全局坐标,在那里进行计算,然后将结果映射回小部件(即您需要它的上下文),这可能是最简单的。

Local (widget) coordinates are relative to the top-left corner of the widget. Global coordinates are screen coordinates. They are easily convertible, and events like QMouseEvent offer both, local (pos()) and global (globalPos()) coordinates.
If you want to map from Widget A to Widget B, you can either do

const QPoint global = a->mapToGlobal( localPosInA );
const QPoint localInB = b->mapFromGlobal( global );

Or, shorter:

const QPoint localInB = a->mapTo( b, localPosInA );

Example: Say you have a top-level widget w1 at (100,110) (screen coordinates), which has a child widget w2 at (10,10) (w1 coordinates) and a mouse event in w2 at (20, 20) (w2 coordinates), then the global position of the mouse cursor is

(100,110) + (10,10) + (20,20) = (130,140) (screen coordinates)

That's w2->mapToGlobal( mousePos ).

w2->mapTo( w1, mousePos ) or, as w1 is parent of w2, w2->mapToParent( mousePos ) is

(10,10) + (20,20) = (30,30) (w1 coordinates).

It might be easiest if you convert everything to global coordinates, do the calculations there, and then map the result back to the widget i.e. context you need it in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文