如何让一个 qwidget 知道另一个 qwidget 位于它之上
我的屏幕上有很多固定的 qwidgets,还有很多其他可以在屏幕上拖动的小部件。当一个小部件被拖动到固定小部件的顶部时,固定小部件需要执行一些代码。我不明白静止物体如何知道它上面有一个小部件并知道它是哪个小部件。
编辑: 我正在拖动的对象是由我自己创建的函数拖动的,而不是 Qt Drag 函数。这就是我使用的
void Piece::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() == Qt::LeftButton && turn == color)
{
x = event->globalX()-18;
y = event->globalY()-18;
move(x,y);
}
}
dropEvent 使用此方法仍然可以工作吗?我尝试制作一个,但是当我将小部件放在固定小部件顶部时,从未输入 dropEvent。
I have a lot of stationary qwidgets on the screen and I have a lot of other widgets that I can drag around the screen. When a widget is dragged on top of the stationary widget, the stationary widget needs to execute some code. I can't figure out how the stationary object can know there is a widget on top of it and know which widget it is.
EDIT:
The object that I'm dragging is being dragged by functions I created myself, not Qt Drag functions. This is what I use
void Piece::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() == Qt::LeftButton && turn == color)
{
x = event->globalX()-18;
y = event->globalY()-18;
move(x,y);
}
}
Will the dropEvent still work using this method? I tried making one but when I dropped the widget on top of the stationary widget the dropEvent was never entered.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
setAcceptDrops(true)
让固定对象接受放置事件,然后实现这些事件以在拖放另一个小部件时执行一些代码:You need to make the stationary object accept drop events using
setAcceptDrops(true)
, then implement these events to execute some code when the other widget is dragged or dropped: