今天主要核心是在几年前,尝试过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); }