简体   繁体   English

如何在Qt中使用自定义titleBar移动窗口

[英]How to move window using custom titleBar in Qt

I'm beginner in Qt and I want to drag and move Window using my own custom titleBar(QLabel).我是 Qt 的初学者,我想使用我自己的自定义 titleBar(QLabel) 拖动和移动 Window。

The Qt code: Qt代码:

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    mpos = event->pos();
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() & Qt::LeftButton) 
    {
        QPoint diff = event->pos() - mpos;
        QPoint newpos = this->pos() + diff;
        this->move(newpos);
    }
}

This code allow me to move window by mouse pressed on any QWidget but I want to move window by mouse pressed on QLabel.这段代码允许我通过在任何 QWidget 上按下鼠标来移动窗口,但我想通过在 QLabel 上按下鼠标来移动窗口。

I know that its kinda late, but I solved this issue.我知道这有点晚了,但我解决了这个问题。 The code is very similar to the implementation that Farhad suggested, but to solve the "jumping" window, you need to update the current position of the mouse also in the event filter:代码与 Farhad 建议的实现非常相似,但要解决“跳跃”窗口,您还需要在事件过滤器中更新鼠标的当前位置:

 if (object == ui->frame_title && event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent* mouseEvent = (QMouseEvent*)event;
            if (pressed == false){
                current = mouseEvent->pos();
            }
            pressed = true;
            return true;
        }

Adding this, you get the current mouse location when the user first press the left-click.添加这个,当用户第一次按下左键时,你会得到当前的鼠标位置。

Here is the full implementation:这是完整的实现:

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    current = event->pos();
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{

    if(pressed)
        this->move(mapToParent(event->pos() - current));
}

bool MainWindow::eventFilter(QObject *object, QEvent *event)
{
    if (object == ui->frame_title && event->type() == QEvent::MouseButtonPress)
    {
        QMouseEvent* mouseEvent = (QMouseEvent*)event;
        if (pressed == false){
            current = mouseEvent->pos();
        }
        pressed = true;
        return true;
    }
    if (object == ui->frame_title && event->type() == QEvent::MouseButtonRelease)
    {
        pressed = false;
        return true;
    }
    else
        return false;
}

Then in your constructor, just add (frame_title is my titlebar):然后在您的构造函数中,只需添加(frame_title 是我的标题栏):

ui->frame_title->installEventFilter(this);

You can re-implement QLabel class and impalement mousePressEvent您可以重新实现 QLabel 类并刺穿mousePressEvent

Example :示例:

header file头文件

#ifndef MYLABLE_H

#define MYLABLE_H

#include <QEvent>
#include <QObject>
#include <QLabel>

class MyLable : public QLabel
{
    Q_OBJECT
public:
    explicit MyLable(QWidget *parent = 0);

    QPoint mpos;

signals:

public slots:



    // QWidget interface
protected:
    void mousePressEvent(QMouseEvent *);
};

#endif // MYLABLE_H

.cpp .cpp

#include "mylable.h"

#include <QMouseEvent>

MyLable::MyLable(QWidget *parent) : QLabel(parent)
{
}

void MyLable::mousePressEvent(QMouseEvent * event)
{
    if (event->buttons() & Qt::LeftButton)
    {
        QPoint diff = event->pos() - mpos;
        QPoint newpos = this->pos() + diff;
        this-> parentWidget()->move(newpos);
    }
}

I suggest you to use eventFilter to get event MousePress and MouseRelease :我建议你使用eventFilter来获取事件MousePressMouseRelease

void MainApp::mousePressEvent(QMouseEvent *event)
{
    current = event->pos();
}

void MainApp::mouseMoveEvent(QMouseEvent *event)
{
    if(pressed)
        this->move(mapToParent(event->pos() - current));
}

bool MainApp::eventFilter(QObject *object, QEvent *event)
{
    if (object == ui->label && event->type() == QEvent::MouseButtonPress)
    {
        pressed = true;
        return true;
    }
    if (object == ui->label && event->type() == QEvent::MouseButtonRelease)
    {
        pressed = false;
        return true;
    }
    else
        return false;
}

This is a sample project for your question on github download here.这是您在github 上下载问题的示例项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM