当鼠标位于 Qt 中窗口的自定义小部件上时,如何移动整个窗口? [英] How to move the whole window when mouse is on the window's custom widget in Qt?

查看:44
本文介绍了当鼠标位于 Qt 中窗口的自定义小部件上时,如何移动整个窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个自定义小部件并将其添加到 qt 的主窗口中.

Let's say I have a custom widget and add it to the main window in qt.

如您所见,红色区域是自定义小部件.我想要做的是当鼠标在红色区域按下并移动时,整个窗口也会移动.

As you can see, the red area is the custom widget. What I want to do is when the mouse is pressed in the red area and moved, the whole window will move as well.

我知道如何简单地实现mousePressEventmouseMoveEvent;但是在处理带有自定义小部件的窗口时,我不知道如何在自定义小部件上按下鼠标时移动整个窗口.

I know how to simply implement mousePressEvent and mouseMoveEvent; but when dealing with a window with the custom widget, I do not know how to move the whole window when mouse is pressed on the custom widget.

另外我想提一下,我只希望在红色区域按下鼠标移动时窗口可移动,而当鼠标在主窗口区域的其余部分按下并移动时,什么都不会发生.

Also I want to mention that I only want the window movable when mouse is pressed and moved in the red area, and when mouse is pressed and moved in the rest part of the main window area, nothing will happen.

这是我的 CustomWidget 类的样子:

This is what my CustomWidget class looks like:

CustomWidget::CustomWidget(QWidget *parent) : QWidget(parent)
{
    setFixedSize(50, 50);
    setStyleSheet("QWidget { background: red; }");
}

void CustomWidget::paintEvent(QPaintEvent *)
{
    QStyleOption opt;
    opt.init(this);
    QPainter painter(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
}

void CustomWidget::mousePressEvent(QMouseEvent *event)
{
    xCoord = event->x();
    yCoord = event->y();
}

void CustomWidget::mouseMoveEvent(QMouseEvent *event)
{
    move(event->globalX() - xCoord, event->globalY() - yCoord);
}

如果你想知道我为什么要这样做,在我的应用程序中,我隐藏了标题栏并自己绘制了一个自定义标题栏.但是窗口是不可移动的,所以我想让整个窗口在鼠标在标题栏上按下和移动时都可以移动.
希望我解释清楚了.

In case you wonder why I want to do this, in my app, I hid the title bar and drew a custom title bar by myself. But the window is not movable, so I want to make the whole window movable when mouse is pressed and moved on the title bar.
Hope I explained myself clearly.

推荐答案

要从任何小部件移动窗口,必须能够访问该窗口,为此我们使用方法 window()即返回顶层,不需要将坐标x()y()分开,以下代码实现解决方案:

To move the window from any widget it is necessary to be able to access the window, and for this we use the method window() that returns the top level, it is not necessary to separate the coordinates x() and y(), the following code implements the solution:

customwidget.h

#ifndef CUSTOMWIDGET_H
#define CUSTOMWIDGET_H

#include <QWidget>

class CustomWidget : public QWidget
{
    Q_OBJECT
public:
    explicit CustomWidget(QWidget *parent = nullptr);
protected:
    void paintEvent(QPaintEvent *);
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
private:
    QPoint startPos;
};

#endif // CUSTOMWIDGET_H

customwidget.cpp

#include "customwidget.h"

#include <QMouseEvent>
#include <QPainter>
#include <QStyleOption>

CustomWidget::CustomWidget(QWidget *parent) : QWidget(parent)
{
    setFixedSize(50, 50);
    setStyleSheet("QWidget { background: red; }");
}

void CustomWidget::paintEvent(QPaintEvent *)
{
    QStyleOption opt;
    opt.init(this);
    QPainter painter(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
}

void CustomWidget::mousePressEvent(QMouseEvent *event)
{
    startPos = event->pos();
    QWidget::mousePressEvent(event);
}

void CustomWidget::mouseMoveEvent(QMouseEvent *event)
{
    QPoint delta = event->pos() - startPos;
    QWidget * w = window();
    if(w)
        w->move(w->pos() + delta);
    QWidget::mouseMoveEvent(event);
}

这篇关于当鼠标位于 Qt 中窗口的自定义小部件上时,如何移动整个窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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