Qt 隐藏标题栏可拖拽,自由缩放

简介: Qt在隐藏标题栏的情况下,实现拖拽很简单,可以看这里https://blog.csdn.net/z609932088/article/details/80865742 或者这里:https://blog.csdn.net/z609932088/article/details/50898022

今天主要核心是在几年前,尝试过Qt在隐藏标题栏情况下实现可自由缩放的效果,


原来的坑在这里:https://blog.csdn.net/z609932088/article/details/53929904


今天这坑终于天上了,看下演示效果 参考链接忘记了,浏览记录里面没有找到,如有侵权,联系我

image.png

大致就是这样的,剩下的看下代码

//头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QMouseEvent>
#include <QApplication>
#include <QDebug>
enum windowEdge{
    TOPLEFT = 11,
    TOP = 12,
    TOPRIGHT = 13,
    LEFT = 21,
    CENTER = 22,
    RIGHT = 23,
    BUTTOMLEFT = 31,
    BUTTOM = 32,
    BUTTOMRIGHT = 33
};
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = nullptr);
signals:
protected:
    /**
     * @brief mousePressEvent
     * @param event
     * 鼠标按下事件
     */
    void mousePressEvent(QMouseEvent *event);
    /**
     * @brief mouseMoveEvent
     * @param event
     * 鼠标移动事件
     */
    void mouseMoveEvent(QMouseEvent *event);
    /**
     * @brief mouseReleaseEvent
     * @param event
     * 鼠标松开事件
     */
    void mouseReleaseEvent(QMouseEvent *event);
    /**
     * @brief mouseDoubleClickEvent
     * @param event
     * 鼠标双击事件
     */
    void mouseDoubleClickEvent(QMouseEvent *event);
    /**
     * @brief setCursorShape
     * @param mPos
     * 设置鼠标形状
     */
    void setCursorShape(int mPos);
    /**
     * @brief calCursorCol
     * @param pt
     * @return
     * 计算鼠标X的位置
     */
    int calCursorCol(QPoint pt);
    /**
     * @brief calCursorPos
     * @param pt
     * @param colPos
     * @return
     * 计算鼠标的位置
     */
    int calCursorPos(QPoint pt,int colPos);
private:
    QPoint m_mousePoint;            //用于存储鼠标位置
    bool m_moveFlag = false;        //窗口移动标志位
    bool m_resizeFlag = false;      //窗口大小重置标志
    const int m_titleHight = 30;    //用于标记标题栏高度
    const int m_frameShape = 2;     //用于鼠标区域判断
    int     m_iCalCursorPos;
    QRect   m_rtPreGeometry;
    QPoint  m_ptViewMousePos;
    QPushButton *m_pushbuttonClose = nullptr;
};
#endif // MAINWINDOW_H
//源文件
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
    this->resize(800,600);
    this->setWindowFlags(Qt::FramelessWindowHint);
    this->setMouseTracking(true);
    m_pushbuttonClose = new QPushButton(this);
    m_pushbuttonClose->setGeometry(100,100,100,80);
    m_pushbuttonClose->setText("关闭");
    //    m_pushbuttonClose->setStyleSheet();
    connect(m_pushbuttonClose,&QPushButton::clicked,this,[=](){this->close();});
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if(event->y() < m_titleHight && event->y()> m_frameShape)
    {
        m_mousePoint = event->globalPos();
        m_moveFlag = true;
    }
    else
    {
        m_iCalCursorPos = calCursorPos(event->pos(),calCursorCol(event->pos()));
        if (event->button() == Qt::LeftButton)
        {
            if(m_iCalCursorPos != CENTER)
            {
                m_resizeFlag = true;
            }
        }
        m_rtPreGeometry = geometry();
        m_ptViewMousePos = event->globalPos();
    }
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if((event->y() < m_titleHight) && event->y()> m_frameShape && m_moveFlag)
    {
        int dx = event->globalX() - m_mousePoint.x();
        int dy = event->globalY() - m_mousePoint.y();
        m_mousePoint = event->globalPos();
        this->move(this->x()+dx,this->y()+dy);
    }
    else
    {
        if(Qt::WindowMaximized != windowState())
        {
            setCursorShape(calCursorPos(event->pos(),calCursorCol(event->pos())));
        }
        QPoint ptCurrentPos = QCursor::pos(); //获取当前的点,这个点是全局的
        QPoint ptMoveSize = ptCurrentPos - m_ptViewMousePos; //计算出移动的位置,当前点 - 鼠标左键按下的点
        QRect rtTempGeometry = m_rtPreGeometry;
        if(m_resizeFlag)
        {
            switch(m_iCalCursorPos)
            {
            case windowEdge::TOPLEFT:
                rtTempGeometry.setTopLeft(m_rtPreGeometry.topLeft()+ptMoveSize);
                break;
            case windowEdge::TOP:
                rtTempGeometry.setTop(m_rtPreGeometry.top()+ptMoveSize.y());
                break;
            case windowEdge::TOPRIGHT:
                rtTempGeometry.setTopRight(m_rtPreGeometry.topRight()+ptMoveSize);
                break;
            case windowEdge::LEFT:
                rtTempGeometry.setLeft(m_rtPreGeometry.left()+ptMoveSize.x());
                break;
            case windowEdge::RIGHT:
                rtTempGeometry.setRight(m_rtPreGeometry.right()+ptMoveSize.x());
                break;
            case windowEdge::BUTTOMLEFT:
                rtTempGeometry.setBottomLeft(m_rtPreGeometry.bottomLeft()+ptMoveSize);
                break;
            case windowEdge::BUTTOM:
                rtTempGeometry.setBottom(m_rtPreGeometry.bottom()+ptMoveSize.y());
                break;
            case windowEdge::BUTTOMRIGHT:
                rtTempGeometry.setBottomRight(m_rtPreGeometry.bottomRight()+ptMoveSize);
                break;
            default:
                break;
            }
            this->setGeometry(rtTempGeometry);
        }
    }
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->y() < m_titleHight && event->y()> m_frameShape && m_moveFlag)
    {
        int dx = event->globalX() - m_mousePoint.x();
        int dy = event->globalY() - m_mousePoint.y();
        m_mousePoint = event->globalPos();
        this->move(this->x()+dx,this->y()+dy);
        m_moveFlag = !m_moveFlag;
    }
    else
    {
        m_resizeFlag = !m_resizeFlag;
        QApplication::restoreOverrideCursor();
    }
}
void MainWindow::mouseDoubleClickEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)       //鼠标双击最大化/正常
    {
        if(event->y() < m_titleHight)
        {
            if(windowState() != Qt::WindowMaximized)
            {
                this->showMaximized();
            }
            else
            {
                this->showNormal();
            }
        }
    }
}
void MainWindow::setCursorShape(int mPos)
{
    Qt::CursorShape mCursor;
    switch (mPos)
    {
    case windowEdge::TOPLEFT:
    case windowEdge::BUTTOMRIGHT:
        mCursor = Qt::SizeFDiagCursor;
        break;
    case windowEdge::TOPRIGHT:
    case windowEdge::BUTTOMLEFT:
        mCursor = Qt::SizeBDiagCursor;
        break;
    case windowEdge::TOP:
    case windowEdge::BUTTOM:
        mCursor = Qt::SizeVerCursor;
        break;
    case windowEdge::LEFT:
    case windowEdge::RIGHT:
        mCursor = Qt::SizeHorCursor;
        break;
    default:
        mCursor = Qt::ArrowCursor;
        break;
    }
    this->setCursor(mCursor);
}
int MainWindow::calCursorCol(QPoint pt)
{
    return (pt.x() < m_frameShape ? 1 : ((pt.x() > this->width() - m_frameShape) ? 3 : 2));
}
int MainWindow::calCursorPos(QPoint pt, int colPos)
{
    return ((pt.y() < m_frameShape ? 10 : ((pt.y() > this->height() - m_frameShape) ? 30 : 20)) + colPos);
}



image.png

目录
相关文章
|
API 计算机视觉
Qt实用技巧:自定义窗口标题栏
Qt实用技巧:自定义窗口标题栏
Qt实用技巧:自定义窗口标题栏
|
计算机视觉
Qt实用技巧:仅去掉标题栏,保持对话框边框
Qt实用技巧:仅去掉标题栏,保持对话框边框
Qt隐藏系统标题栏,使用自定义标题栏
Qt隐藏系统标题栏,使用自定义标题栏
Qt隐藏系统标题栏,使用自定义标题栏
Qt-网易云音乐界面实现-1 窗口隐藏拖拽移动,自定义标题栏
最近也换了公司,也换了新的工作,工资也象征性的涨了一点点,但是最近心里还是慌慌,不知道为什么,没有那种踏实感,感觉自己随时可以被抛弃的感觉。感觉自己在荒废时间,也感觉自己在浪费生命。
300 0
Qt-网易云音乐界面实现-1 窗口隐藏拖拽移动,自定义标题栏
|
C++
Qt Qwdget 汽车仪表知识点拆解5 标题栏图标闪烁
先贴上效果图,注意,没有写逻辑,都是乱动的
144 0
Qt Qwdget 汽车仪表知识点拆解5 标题栏图标闪烁
|
搜索推荐 C++
Qt 个性化标题栏,自定义标题栏
目前还没有达到自己满意的地步,魔方别人写的的,先提供参考,后面在加入新的东西
250 0
Qt 个性化标题栏,自定义标题栏
QT软件开发:解决隐藏QWidget标题栏后窗体无法移动问题
QT软件开发:解决隐藏QWidget标题栏后窗体无法移动问题
401 0
QT隐藏窗口标题栏、最大化、最小化、关闭按钮
QT隐藏窗口标题栏、最大化、最小化、关闭按钮
1376 0
QT 标题栏隐藏可拖拽
这个也是我网上找到了 为了方便,记录一下
131 0

推荐镜像

更多