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

今天這坑終於天上了,看下演示效果 參考鏈接忘記了,瀏覽記錄裏面沒有找到,如有侵權,聯繫我

大致就是這樣的,剩下的看下代碼

 

//頭文件

#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);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章