移动/拖动一个FramelessWindow? [Qt] [英] Moving/dragging a FramelessWindow? [Qt]

查看:163
本文介绍了移动/拖动一个FramelessWindow? [Qt]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的应用程序是Windowless( setWindowFlags(Qt :: FramelessWindowHint); ),并有一个 QPushButton

I have a very simple application that is Windowless (setWindowFlags(Qt::FramelessWindowHint);) and has one QPushButton.

我使整个窗口可以通过以下方式拖动代码:

I made the entire window draggable with the following code:

void myApp::mousePressEvent(QMouseEvent* event){
  m_nMouseClick_X_Coordinate = event->x();
  m_nMouseClick_Y_Coordinate = event->y();
  qDebug() << m_nMouseClick_X_Coordinate;
  qDebug() << m_nMouseClick_Y_Coordinate;
}

void myApp::mouseMoveEvent(QMouseEvent* event){
  move(event->globalX()-m_nMouseClick_X_Coordinate,event->globalY()-m_nMouseClick_Y_Coordinate);
  qDebug() << event->globalX();
  qDebug() << event->globalY();
}

当我拖动任何地方时,窗口被拖动得很好 >在 QPushButton 上。如果我开始拖动按钮(单击并按住), qDebug 不输出任何内容。当我开始拖动它,窗口移动,但拖动源跳转到上一个拖动开始的地方的坐标,光标也跳到最后一个拖动的位置。有没有办法让它开始拖动 QPushButton 不会导致这种行为?当 QPushButton 是拖动的开始时,坐标似乎不被捕获。

The window gets dragged fine when I drag anywhere except on the QPushButton. If I start the drag on the button (click and hold), the qDebug outputs nothing. When I start to drag it, the window moves, but the drag origin jumps to the coordinates of wherever the previous drag started and the cursor also jumps to the position of the last drag. Is there a way to make it so that starting the drag on the QPushButton doesn't cause this behavior? The coordinates don't seem to get captured when the QPushButton is the beginning of the drag.

提前感谢。

请参阅下面的@Hi I'm Frogatto的回复。它给出了一个很好的大纲如何做到实现和让它工作。我在构造函数中安装事件过滤器到按钮:

See the response by @Hi I'm Frogatto below. It gives a good outline of how to do implement this and get it working. I wound up installing the event filter to the pushbutton in the constructor:

ui-> gt; pushButton-> installEventFilter(this) code>

ui->pushButton->installEventFilter(this);

然后我创建了我的eventfilter:

Then I created my eventfilter:

bool myApp::eventFilter(QObject *obj, QEvent *event)
{
  if (obj == ui->pushButton && event->type()==QMouseEvent::MouseButtonPress){
    QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
//    m_nMouseClick_X_Coordinate = mouseEvent->x() + 100;
//    m_nMouseClick_Y_Coordinate = mouseEvent->y() + 90;
    absPosX = ui->pushButton->mapToParent(QPoint(0,0)).x();
    absPosY = ui->pushButton->mapToParent(QPoint(0,0)).y();
    m_nMouseClick_X_Coordinate = mouseEvent->x() + absPosX;
    m_nMouseClick_Y_Coordinate = mouseEvent->y() + absPosY;
    return true;
  }
  return false;
}

注意,我注释掉了我用硬值更新两个变量的部分。我不想每次都要硬编码按钮的位置,所以我使用 mapToParent 定义了按钮的一些绝对位置。

Note that I commented out the section where I update the two variables with hard values. I didn't want to have to hard code the location of the button every time, so I defined some absolute positions of the button using mapToParent.

推荐答案

看来, QPushButton 吸收了新闻事件,不会将其传递到其父窗口小部件。因此在这种情况下,父小部件将不会收到鼠标按下事件的通知。解决此问题的一个简单方法是使用Qt 事件过滤器。您可以在 QPushButton 上安装一个事件过滤器,可以将其视为此按钮的所有事件的入口点。

It appears that the QPushButton absorbs the press event and does not deliver it to its parent widget. So in this case the parent widget will not get notified of that mouse press event. A simple solution to this problem is using Qt event filters. You could install an event filter on that QPushButton which could be thought of as an entry point for all events to this button.

因此,工作流程如下:


  1. 用户按鼠标点击按钮。

  2. 我们的事件过滤器更新了 m_nMouseClick_X_Coordinate m_nMouseClick_Y_Coordinate 变量。


  1. User presses the mouse click on the button.
  2. Mouse press event is delivered to our event filter.
  3. Our event filter updates m_nMouseClick_X_Coordinate and m_nMouseClick_Y_Coordinate variables. And then redirects the event to the button.
  4. The button receives the event...

>现在,我们应该:


  • 定义我们的事件过滤器类。

  • 方法。

我将把其余部分留给你做练习。 )

I will leave the rest to you as an exercise. :)

这篇关于移动/拖动一个FramelessWindow? [Qt]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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