当前位置:   article > 正文

QT窗口缩放,自定义边框,无边框缩放拉伸_qt mainwindow 左右拖动缩放界面

qt mainwindow 左右拖动缩放界面

目录

什么情况下需要自定义边框?

效果图

一、基本思路

二、参考程序一

2.1 源码

2.2 思路说明

2.3 缺点说明

三、参考程序2

3.1 源码

3.2 思路说明

3.3 优缺点说明

四、参考程序3

4.1源码

4.2 思路说明

4.3 优缺点说明

4.4 GitHub

五、结语


什么情况下需要自定义边框?

  由于QT对修改标题栏样式的支持有限,处于美化的考虑常常会使用使用无边框模式,并自己设计标题栏样式。

  使用 setWindowFlag(Qt::FramelessWindowHint);  关闭QT窗口边框后,也会导致窗口拉伸缩放功能失效。

  因此,需要使用自定义边框的方式,来实现窗口的拉升缩放。

效果图

自定义边框拉伸效果演示

一、基本思路

  对于自定义边框样式,网上的方案有很多,但是归根结底都是一个基本思路

  捕获鼠标位置→在指定位置显示指定的鼠标样式→点击后捕获当前的点击位置→鼠标移动,依据点击的位置,计算位移→将位移反馈给窗体→窗体依据位移进行缩放拉伸→鼠标释放,释放捕获。

二、参考程序一

2.1 源码

MyBorder.h

  1. #ifndef MYLABEL_H
  2. #define MYLABEL_H
  3. #include <QObject>
  4. #include <QLabel>
  5. #include <QMouseEvent>
  6. #include <qnamespace.h>
  7. class MyBorder:public QLabel
  8. {
  9. Q_OBJECT
  10. public:
  11. MyBorder(QWidget *parent=nullptr);
  12. void setScaleCursor(int nshape = 0);
  13. void mousePressEvent(QMouseEvent *ev);
  14. void mouseMoveEvent(QMouseEvent *ev);
  15. void mouseReleaseEvent(QMouseEvent *ev);
  16. QPoint m_mousePointOld;
  17. bool m_bKeepDrag;
  18. signals:
  19. void moveEventSig(QPoint point);
  20. void mouseReleasedSig();
  21. };
  22. #endif // MYLABEL_H

MyBorder.cpp

  1. #include "MyBorder.h"
  2. MyBorder::MyBorder(QWidget *parent):m_bKeepDrag(false)
  3. {
  4. this->setParent(parent);
  5. }
  6. void MyBorder::setScaleCursor(int nshape)
  7. {
  8. if(nshape == 1)//左右拉伸
  9. {
  10. setCursor(Qt::SizeHorCursor); //改变光标形状
  11. }
  12. else if(nshape == 2)//上下拉伸
  13. {
  14. setCursor(Qt::SizeVerCursor);
  15. }
  16. else if(nshape == 3)//右下拉伸
  17. {
  18. setCursor(Qt::SizeFDiagCursor);
  19. }
  20. else if(nshape == 4)//左下拉伸
  21. {
  22. setCursor(Qt::SizeBDiagCursor);
  23. }
  24. else //正常显示
  25. {
  26. setCursor(Qt::ArrowCursor);
  27. }
  28. }
  29. void MyBorder::mousePressEvent(QMouseEvent *ev)
  30. {
  31. if(ev->button() == Qt::LeftButton)
  32. {
  33. m_bKeepDrag = true; //拉伸标志
  34. m_mousePointOld = ev->globalPos();
  35. }
  36. }
  37. void MyBorder::mouseMoveEvent(QMouseEvent *ev)
  38. {
  39. if(m_bKeepDrag)
  40. {
  41. const QPoint position = ev->globalPos() - m_mousePointOld; //the position of mainfrmae + (current_mouse_position - last_mouse_position)
  42. //move(position.x(), position.y());
  43. emit moveEventSig(position);
  44. m_mousePointOld = ev->globalPos();
  45. }
  46. }
  47. void MyBorder::mouseReleaseEvent(QMouseEvent *ev)
  48. {
  49. Q_UNUSED(ev)
  50. m_bKeepDrag = false;
  51. }

mainwindow.h

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QMainWindow>
  4. #include <MyBorder/MyBorder.h>
  5. QT_BEGIN_NAMESPACE
  6. namespace Ui { class MainWindow; }
  7. QT_END_NAMESPACE
  8. class MainWindow : public QMainWindow
  9. {
  10. Q_OBJECT
  11. public:
  12. MainWindow(QWidget *parent = nullptr);
  13. ~MainWindow();
  14. private slots:
  15. void getLeftScaleEvent(QPoint movPoint);
  16. void getRightScaleEvent(QPoint movPoint);
  17. void getBottomScaleEvent(QPoint movPoint);
  18. void getTopScaleEvent(QPoint movPoint);
  19. void getRBScaleEvent(QPoint movPoint);
  20. void getRTScaleEvent(QPoint movPoint);
  21. void getLTScaleEvent(QPoint movPoint);
  22. void getLBScaleEvent(QPoint movPoint);
  23. private:
  24. void DarwBorder();
  25. void InitBorder();
  26. uint16_t minWindowHeight; //最小窗口高度
  27. uint16_t minWindowWidth; //最小窗口宽度
  28. //边框控件
  29. MyBorder *labelLft;
  30. MyBorder *labelRit;
  31. MyBorder *labelBot;
  32. MyBorder *labelTop;
  33. MyBorder *labelRB;
  34. MyBorder *labelRT;
  35. MyBorder *labelLB;
  36. MyBorder *labelLT;
  37. Ui::MainWindow *ui;
  38. };
  39. #endif // MAINWINDOW_H

mainwindow.cpp

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. MainWindow::MainWindow(QWidget *parent)
  4. : QMainWindow(parent)
  5. , ui(new Ui::MainWindow)
  6. {
  7. ui->setupUi(this);
  8. setWindowFlag(Qt::FramelessWindowHint);
  9. InitBorder();
  10. }
  11. MainWindow::~MainWindow()
  12. {
  13. delete ui;
  14. }
  15. //初始化左右边框
  16. void MainWindow::InitBorder()
  17. {
  18. minWindowWidth = 1000; //最小窗口宽度
  19. minWindowHeight = 650; //最小窗口高度
  20. //上下左右的label,为了控制界面能够拖动拉伸
  21. labelLft = new MyBorder(this);
  22. labelLft->setStyleSheet("QLabel {background-color: transparent;}");//设置背景透明
  23. labelLft->raise();
  24. labelLft->setScaleCursor(1);
  25. labelRit = new MyBorder(this);
  26. labelRit->setStyleSheet("QLabel {background-color: transparent;}");//设置背景透明
  27. labelRit->raise();
  28. labelRit->setScaleCursor(1);//设置为左右拉升光标
  29. labelBot = new MyBorder(this);
  30. labelBot->setStyleSheet("QLabel {background-color: transparent;}");//设置背景透明
  31. labelBot->raise();
  32. labelBot->setScaleCursor(2);//设置为上下拉升光标
  33. labelTop = new MyBorder(this);
  34. labelTop->setStyleSheet("QLabel {background-color: transparent;}");//设置背景透明
  35. labelTop->setScaleCursor(2);//设置为上下拉升光标
  36. labelRB = new MyBorder(this);
  37. labelRB->setStyleSheet("QLabel {background-color: transparent;}");//设置背景透明
  38. labelRB->setScaleCursor(3);//设置为右下拉升光标
  39. labelRT = new MyBorder(this);
  40. labelRT->setStyleSheet("QLabel {background-color: transparent;}");//设置背景透明
  41. labelRT->setScaleCursor(4);//设置为右上拉升光标
  42. labelLB = new MyBorder(this);
  43. labelLB->setStyleSheet("QLabel {background-color: transparent;}");//设置背景透明
  44. labelLB->setScaleCursor(4);//设置为左下拉升光标
  45. labelLT = new MyBorder(this);
  46. labelLT->setStyleSheet("QLabel {background-color: transparent;}");//设置背景透明
  47. labelLT->setScaleCursor(3);//设置为左上拉升光标
  48. //绘制边框
  49. DarwBorder();
  50. //关联回调函数
  51. connect(labelLft, SIGNAL(moveEventSig(QPoint)), this, SLOT(getLeftScaleEvent(QPoint)));
  52. connect(labelRit, SIGNAL(moveEventSig(QPoint)), this, SLOT(getRightScaleEvent(QPoint)));
  53. connect(labelBot, SIGNAL(moveEventSig(QPoint)), this, SLOT(getBottomScaleEvent(QPoint)));
  54. connect(labelTop, SIGNAL(moveEventSig(QPoint)), this, SLOT(getTopScaleEvent(QPoint)));
  55. connect(labelRB, SIGNAL(moveEventSig(QPoint)), this, SLOT(getRBScaleEvent(QPoint)));
  56. connect(labelRT, SIGNAL(moveEventSig(QPoint)), this, SLOT(getRTScaleEvent(QPoint)));
  57. connect(labelLB, SIGNAL(moveEventSig(QPoint)), this, SLOT(getLBScaleEvent(QPoint)));
  58. connect(labelLT, SIGNAL(moveEventSig(QPoint)), this, SLOT(getLTScaleEvent(QPoint)));
  59. }
  60. //重绘边框
  61. void MainWindow::DarwBorder()
  62. {
  63. labelLft->setGeometry(0,0,5,this->height());
  64. labelRit->setGeometry(this->width()-5,0,5,this->height());
  65. labelBot->setGeometry(0,this->height()-5,this->width(),5);
  66. labelTop->setGeometry(0,0,this->width(),5);
  67. labelRB->setGeometry(this->width()-6,this->height()-6,this->width(),this->height());
  68. labelRT->setGeometry(this->width()-6,0,this->width(),6);
  69. labelLB->setGeometry(0,this->height()-6,6,this->width());
  70. labelLT->setGeometry(0,0,6,6);
  71. }
  72. void MainWindow::getLeftScaleEvent(QPoint movPoint)
  73. {
  74. if((pos().x()+movPoint.x())>(pos().x()+this->width()-minWindowWidth))
  75. {
  76. return;//保证拖动窗口左边界的时候,控件宽度至少有200
  77. }
  78. this->setGeometry(pos().x()+movPoint.x(),pos().y(),this->width()-movPoint.x(),this->height());
  79. DarwBorder();
  80. }
  81. void MainWindow::getRightScaleEvent(QPoint movPoint)
  82. {
  83. if((pos().x()+this->width()+movPoint.x())<(pos().x()+minWindowWidth))
  84. {
  85. return;//保证拖动窗口右边界的时候,控件宽度至少有200
  86. }
  87. this->setGeometry(pos().x(),pos().y(),this->width()+movPoint.x(),this->height());
  88. DarwBorder();
  89. }
  90. void MainWindow::getBottomScaleEvent(QPoint movPoint)
  91. {
  92. if((pos().y()+this->height()+movPoint.y())<(pos().y()+minWindowHeight))
  93. {
  94. return;//保证拖动窗口下边界的时候,控件高度至少有200
  95. }
  96. this->setGeometry(pos().x(),pos().y(),this->width(),this->height()+movPoint.y());
  97. DarwBorder();
  98. }
  99. void MainWindow::getTopScaleEvent(QPoint movPoint)
  100. {
  101. if((pos().y()+movPoint.y())>(pos().y()+this->height()-minWindowHeight))
  102. {
  103. return;//保证拖动窗口上边界的时候,控件高度至少有200
  104. }
  105. this->setGeometry(pos().x(),pos().y()+movPoint.y(),this->width(),this->height()-movPoint.y());
  106. DarwBorder();
  107. }
  108. void MainWindow::getRBScaleEvent(QPoint movPoint)
  109. {
  110. if((pos().y()+this->height()+movPoint.y())<(pos().y()+minWindowHeight)
  111. || (pos().x()+this->width()+movPoint.x())<(pos().x()+minWindowWidth))
  112. {
  113. return;//保证拖动窗口上边界的时候,控件高度和宽度至少有200
  114. }
  115. this->setGeometry(pos().x(),pos().y(),this->width()+movPoint.x(),this->height());
  116. this->setGeometry(pos().x(),pos().y(),this->width(),this->height()+movPoint.y());
  117. DarwBorder();
  118. }
  119. void MainWindow::getRTScaleEvent(QPoint movPoint)
  120. {
  121. if((pos().x()+this->width()+movPoint.x())<(pos().x()+minWindowWidth)
  122. || (pos().y()+movPoint.y())>(pos().y()+this->height()-minWindowHeight) )
  123. {
  124. return;//保证拖动窗口上边界的时候,控件高度和宽度至少有200
  125. }
  126. this->setGeometry(pos().x(),pos().y()+movPoint.y(),this->width(),this->height()-movPoint.y());
  127. this->setGeometry(pos().x(),pos().y(),this->width()+movPoint.x(),this->height());
  128. DarwBorder();
  129. }
  130. void MainWindow::getLTScaleEvent(QPoint movPoint)
  131. {
  132. if((pos().x()+movPoint.x())>(pos().x()+this->width()-minWindowWidth)
  133. || (pos().y()+movPoint.y())>(pos().y()+this->height()-minWindowHeight) )
  134. {
  135. return;//保证拖动窗口上边界的时候,控件高度和宽度至少有200
  136. }
  137. this->setGeometry(pos().x()+movPoint.x(),pos().y(),this->width()-movPoint.x(),this->height());
  138. this->setGeometry(pos().x(),pos().y()+movPoint.y(),this->width(),this->height()-movPoint.y());
  139. DarwBorder();
  140. }
  141. void MainWindow::getLBScaleEvent(QPoint movPoint)
  142. {
  143. if((pos().x()+movPoint.x())>(pos().x()+this->width()-minWindowWidth)
  144. || (pos().y()+this->height()+movPoint.y())<(pos().y()+minWindowHeight))
  145. {
  146. return;//保证拖动窗口上边界的时候,控件高度和宽度至少有200
  147. }
  148. this->setGeometry(pos().x()+movPoint.x(),pos().y(),this->width()-movPoint.x(),this->height());
  149. this->setGeometry(pos().x(),pos().y(),this->width(),this->height()+movPoint.y());
  150. DarwBorder();
  151. }

2.2 思路说明

  通过重写QLabel中的鼠标点击事件,鼠标移动事件,鼠标释放事件三个事件,并将QLabel绘制在主窗口的特定位置。特定事件产生时,将相关参数通过信号的方式传递给主窗口,从而控制主窗口进行拉伸缩放。

2.3 缺点说明

  使用这种方式,能够实现自定义缩放功能没错,但是也有明显的缺点,和直接重现主窗口中的三个鼠标事件的方式一样(网上有这种方式),每个多一个新窗口都需要在新窗口的代码中重写一次初始化函数,关联回调函数等操作,就算是复制粘贴,窗口一多也会显得十分的麻烦。

  因此,我们需要加入一点点细节,来解决这个问题,下面请看参考程序2。

三、参考程序2

3.1 源码

MyBorder.h

  1. #ifndef MYLABEL_H
  2. #define MYLABEL_H
  3. #include <QObject>
  4. #include <QLabel>
  5. #include <QMouseEvent>
  6. #include <qnamespace.h>
  7. class MyBorder:public QLabel
  8. {
  9. Q_OBJECT
  10. public:
  11. MyBorder(QWidget *parent=nullptr);
  12. void setScaleCursor(int nshape = 0);
  13. void mousePressEvent(QMouseEvent *ev);
  14. void mouseMoveEvent(QMouseEvent *ev);
  15. void mouseReleaseEvent(QMouseEvent *ev);
  16. QPoint m_mousePointOld;
  17. bool m_bKeepDrag;
  18. signals:
  19. void moveEventSig(QPoint point);
  20. void mouseReleasedSig();
  21. };
  22. #endif // MYLABEL_H

MyBorder.cpp

  1. #include "MyBorder.h"
  2. MyBorder::MyBorder(QWidget *parent):m_bKeepDrag(false)
  3. {
  4. this->setParent(parent);
  5. }
  6. void MyBorder::setScaleCursor(int nshape)
  7. {
  8. if(nshape == 1)//左右拉伸
  9. {
  10. setCursor(Qt::SizeHorCursor); //改变光标形状
  11. }
  12. else if(nshape == 2)//上下拉伸
  13. {
  14. setCursor(Qt::SizeVerCursor);
  15. }
  16. else if(nshape == 3)//右下拉伸
  17. {
  18. setCursor(Qt::SizeFDiagCursor);
  19. }
  20. else if(nshape == 4)//左下拉伸
  21. {
  22. setCursor(Qt::SizeBDiagCursor);
  23. }
  24. else //正常显示
  25. {
  26. setCursor(Qt::ArrowCursor);
  27. }
  28. }
  29. void MyBorder::mousePressEvent(QMouseEvent *ev)
  30. {
  31. if(ev->button() == Qt::LeftButton)
  32. {
  33. m_bKeepDrag = true;
  34. m_mousePointOld = ev->globalPos();
  35. }
  36. }
  37. void MyBorder::mouseMoveEvent(QMouseEvent *ev)
  38. {
  39. if(m_bKeepDrag)
  40. {
  41. const QPoint position = ev->globalPos() - m_mousePointOld; //the position of mainfrmae + (current_mouse_position - last_mouse_position)
  42. //move(position.x(), position.y());
  43. emit moveEventSig(position);
  44. m_mousePointOld = ev->globalPos();
  45. }
  46. }
  47. void MyBorder::mouseReleaseEvent(QMouseEvent *ev)
  48. {
  49. Q_UNUSED(ev)
  50. m_bKeepDrag = false;
  51. }

MyBorderContainer.h

  1. #ifndef MYBORDERCONTAINER_H
  2. #define MYBORDERCONTAINER_H
  3. #include "MyBorder.h"
  4. #include <QWidget>
  5. #include <QObject>
  6. #include <QLabel>
  7. #include <QMouseEvent>
  8. class MyBorderContainer : public QObject
  9. {
  10. Q_OBJECT
  11. public:
  12. MyBorderContainer(QWidget *parent,uint16_t minWindowHeight = 50, uint16_t minWindowWidth = 50, uint16_t borderSize = 5);
  13. void setMinWindowSize(uint16_t minWindowWidth, uint16_t minWindowHeight); //设置窗口最小尺寸
  14. void setBorderSize(uint16_t borderSize); //设置边框捕获区域尺寸
  15. void DarwBorder();
  16. protected:
  17. void InitBorder();
  18. private slots:
  19. void getLeftScaleEvent(QPoint movPoint);
  20. void getRightScaleEvent(QPoint movPoint);
  21. void getBottomScaleEvent(QPoint movPoint);
  22. void getTopScaleEvent(QPoint movPoint);
  23. void getRBScaleEvent(QPoint movPoint);
  24. void getRTScaleEvent(QPoint movPoint);
  25. void getLTScaleEvent(QPoint movPoint);
  26. void getLBScaleEvent(QPoint movPoint);
  27. private:
  28. uint16_t minWindowHeight; //最小窗口高度
  29. uint16_t minWindowWidth; //最小窗口宽度
  30. uint16_t borderSize;
  31. QWidget *forms;
  32. //边框控件
  33. MyBorder *labelLft;
  34. MyBorder *labelRit;
  35. MyBorder *labelBot;
  36. MyBorder *labelTop;
  37. MyBorder *labelRB;
  38. MyBorder *labelRT;
  39. MyBorder *labelLB;
  40. MyBorder *labelLT;
  41. };
  42. #endif // MYBORDERCONTAINER_H

MyBorderContainer.cpp

  1. #include "MyBorderContainer.h"
  2. MyBorderContainer::MyBorderContainer(QWidget *parent,uint16_t minWindowHeight,uint16_t minWindowWidth, uint16_t borderSize)
  3. {
  4. forms = parent;
  5. this->minWindowHeight = minWindowHeight; //最小窗口高度
  6. this->minWindowWidth = minWindowWidth; //最小窗口宽度
  7. this->borderSize = borderSize;
  8. this->setParent(parent);
  9. InitBorder();
  10. }
  11. void MyBorderContainer::InitBorder()
  12. {
  13. //上下左右的label,为了控制界面能够拖动拉伸
  14. labelLft = new MyBorder(forms);
  15. labelLft->setStyleSheet("QLabel {background-color: transparent;}");//设置背景透明
  16. labelLft->raise();
  17. labelLft->setScaleCursor(1);
  18. connect(labelLft, SIGNAL(moveEventSig(QPoint)), this, SLOT(getLeftScaleEvent(QPoint)));
  19. labelRit = new MyBorder(forms);
  20. labelRit->setStyleSheet("QLabel {background-color: transparent;}");//设置背景透明
  21. labelRit->raise();
  22. labelRit->setScaleCursor(1);//设置为左右拉升光标
  23. connect(labelRit, SIGNAL(moveEventSig(QPoint)), this, SLOT(getRightScaleEvent(QPoint)));
  24. labelBot = new MyBorder(forms);
  25. labelBot->setStyleSheet("QLabel {background-color: transparent;}");//设置背景透明
  26. labelBot->raise();
  27. labelBot->setScaleCursor(2);//设置为上下拉升光标
  28. connect(labelBot, SIGNAL(moveEventSig(QPoint)), this, SLOT(getBottomScaleEvent(QPoint)));
  29. labelTop = new MyBorder(forms);
  30. labelTop->setStyleSheet("QLabel {background-color: transparent;}");//设置背景透明
  31. labelTop->raise();
  32. labelTop->setScaleCursor(2);//设置为上下拉升光标
  33. connect(labelTop, SIGNAL(moveEventSig(QPoint)), this, SLOT(getTopScaleEvent(QPoint)));
  34. labelRB = new MyBorder(forms);
  35. labelRB->setStyleSheet("QLabel {background-color: transparent;}");//设置背景透明
  36. labelRB->raise();
  37. labelRB->setScaleCursor(3);//设置为右下拉升光标
  38. connect(labelRB, SIGNAL(moveEventSig(QPoint)), this, SLOT(getRBScaleEvent(QPoint)));
  39. labelRT = new MyBorder(forms);
  40. labelRT->setStyleSheet("QLabel {background-color: transparent;}");//设置背景透明
  41. labelRT->raise();
  42. labelRT->setScaleCursor(4);//设置为右上拉升光标
  43. connect(labelRT, SIGNAL(moveEventSig(QPoint)), this, SLOT(getRTScaleEvent(QPoint)));
  44. labelLB = new MyBorder(forms);
  45. labelLB->setStyleSheet("QLabel {background-color: transparent;}");//设置背景透明
  46. labelLB->raise();
  47. labelLB->setScaleCursor(4);//设置为左下拉升光标
  48. connect(labelLB, SIGNAL(moveEventSig(QPoint)), this, SLOT(getLBScaleEvent(QPoint)));
  49. labelLT = new MyBorder(forms);
  50. labelLT->setStyleSheet("QLabel {background-color: transparent;}");//设置背景透明
  51. labelLT->raise();
  52. labelLT->setScaleCursor(3);//设置为左上拉升光标
  53. connect(labelLT, SIGNAL(moveEventSig(QPoint)), this, SLOT(getLTScaleEvent(QPoint)));
  54. DarwBorder();
  55. }
  56. void MyBorderContainer::setMinWindowSize(uint16_t minWindowWidth, uint16_t minWindowHeight)
  57. {
  58. this->minWindowHeight = minWindowHeight; //最小窗口高度
  59. this->minWindowWidth = minWindowWidth; //最小窗口宽度
  60. }
  61. void MyBorderContainer::setBorderSize(uint16_t borderSize)
  62. {
  63. this->borderSize = borderSize;
  64. }
  65. //重绘边框
  66. void MyBorderContainer::DarwBorder()
  67. {
  68. labelLft->setGeometry(0,0,this->borderSize,forms->height());
  69. labelRit->setGeometry(forms->width()-this->borderSize,0,this->borderSize,forms->height());
  70. labelBot->setGeometry(0,forms->height()-this->borderSize,forms->width(),this->borderSize);
  71. labelTop->setGeometry(0,0,forms->width(),this->borderSize);
  72. labelRB->setGeometry(forms->width()-this->borderSize-1,forms->height()-this->borderSize-1,forms->width(),forms->height());
  73. labelRT->setGeometry(forms->width()-this->borderSize-1,0,forms->width(),this->borderSize+1);
  74. labelLB->setGeometry(0,forms->height()-this->borderSize-1,this->borderSize+1,forms->width());
  75. labelLT->setGeometry(0,0,this->borderSize+1,this->borderSize+1);
  76. }
  77. void MyBorderContainer::getLeftScaleEvent(QPoint movPoint)
  78. {
  79. if((forms->pos().x()+movPoint.x())>(forms->pos().x()+forms->width()-minWindowWidth))
  80. {
  81. return;//保证拖动窗口左边界的时候,控件宽度至少有最小值
  82. }
  83. forms->setGeometry(forms->pos().x()+movPoint.x(),forms->pos().y(),forms->width()-movPoint.x(),forms->height());
  84. DarwBorder();
  85. }
  86. void MyBorderContainer::getRightScaleEvent(QPoint movPoint)
  87. {
  88. if((forms->pos().x()+forms->width()+movPoint.x())<(forms->pos().x()+minWindowWidth))
  89. {
  90. return;//保证拖动窗口右边界的时候,控件宽度至少有最小值
  91. }
  92. forms->setGeometry(forms->pos().x(),forms->pos().y(),forms->width()+movPoint.x(),forms->height());
  93. DarwBorder();
  94. }
  95. void MyBorderContainer::getBottomScaleEvent(QPoint movPoint)
  96. {
  97. if((forms->pos().y()+forms->height()+movPoint.y())<(forms->pos().y()+minWindowHeight))
  98. {
  99. return;//保证拖动窗口下边界的时候,控件高度至少有最小值
  100. }
  101. forms->setGeometry(forms->pos().x(),forms->pos().y(),forms->width(),forms->height()+movPoint.y());
  102. DarwBorder();
  103. }
  104. void MyBorderContainer::getTopScaleEvent(QPoint movPoint)
  105. {
  106. if((forms->pos().y()+movPoint.y())>(forms->pos().y()+forms->height()-minWindowHeight))
  107. {
  108. return;//保证拖动窗口上边界的时候,控件高度至少有最小值
  109. }
  110. forms->setGeometry(forms->pos().x(),forms->pos().y()+movPoint.y(),forms->width(),forms->height()-movPoint.y());
  111. DarwBorder();
  112. }
  113. void MyBorderContainer::getRBScaleEvent(QPoint movPoint)
  114. {
  115. if((forms->pos().y()+forms->height()+movPoint.y())<(forms->pos().y()+minWindowHeight)
  116. || (forms->pos().x()+forms->width()+movPoint.x())<(forms->pos().x()+minWindowWidth))
  117. {
  118. return;//保证拖动窗口上边界的时候,控件高度和宽度至少有最小值
  119. }
  120. forms->setGeometry(forms->pos().x(),forms->pos().y(),forms->width()+movPoint.x(),forms->height());
  121. forms->setGeometry(forms->pos().x(),forms->pos().y(),forms->width(),forms->height()+movPoint.y());
  122. DarwBorder();
  123. }
  124. void MyBorderContainer::getRTScaleEvent(QPoint movPoint)
  125. {
  126. if((forms->pos().x()+forms->width()+movPoint.x())<(forms->pos().x()+minWindowWidth)
  127. || (forms->pos().y()+movPoint.y())>(forms->pos().y()+forms->height()-minWindowHeight) )
  128. {
  129. return;//保证拖动窗口上边界的时候,控件高度和宽度至少有最小值
  130. }
  131. forms->setGeometry(forms->pos().x(),forms->pos().y()+movPoint.y(),forms->width(),forms->height()-movPoint.y());
  132. forms->setGeometry(forms->pos().x(),forms->pos().y(),forms->width()+movPoint.x(),forms->height());
  133. DarwBorder();
  134. }
  135. void MyBorderContainer::getLTScaleEvent(QPoint movPoint)
  136. {
  137. if((forms->pos().x()+movPoint.x())>(forms->pos().x()+forms->width()-minWindowWidth)
  138. || (forms->pos().y()+movPoint.y())>(forms->pos().y()+forms->height()-minWindowHeight) )
  139. {
  140. return;//保证拖动窗口上边界的时候,控件高度和宽度至少有最小值
  141. }
  142. forms->setGeometry(forms->pos().x()+movPoint.x(),forms->pos().y(),forms->width()-movPoint.x(),forms->height());
  143. forms->setGeometry(forms->pos().x(),forms->pos().y()+movPoint.y(),forms->width(),forms->height()-movPoint.y());
  144. DarwBorder();
  145. }
  146. void MyBorderContainer::getLBScaleEvent(QPoint movPoint)
  147. {
  148. if((forms->pos().x()+movPoint.x())>(forms->pos().x()+forms->width()-minWindowWidth)
  149. || (forms->pos().y()+forms->height()+movPoint.y())<(forms->pos().y()+minWindowHeight))
  150. {
  151. return;//保证拖动窗口上边界的时候,控件高度和宽度至少有最小值
  152. }
  153. forms->setGeometry(forms->pos().x()+movPoint.x(),forms->pos().y(),forms->width()-movPoint.x(),forms->height());
  154. forms->setGeometry(forms->pos().x(),forms->pos().y(),forms->width(),forms->height()+movPoint.y());
  155. DarwBorder();
  156. }

mainwindow.h

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <MyBorder/MyBorderContainer.h>
  4. #include <QMainWindow>
  5. namespace Ui {
  6. class MainWindow;
  7. }
  8. class MainWindow : public QMainWindow
  9. {
  10. Q_OBJECT
  11. public:
  12. explicit MainWindow(QWidget *parent = 0);
  13. ~MainWindow();
  14. private:
  15. Ui::MainWindow *ui;
  16. private:
  17. MyBorderContainer *My;
  18. };
  19. #endif // MAINWINDOW_H

mainwindow.cpp

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. MainWindow::MainWindow(QWidget *parent) :
  4. QMainWindow(parent),
  5. ui(new Ui::MainWindow)
  6. {
  7. ui->setupUi(this);
  8. setWindowFlag(Qt::FramelessWindowHint);
  9. My = new MyBorderContainer(this);
  10. }
  11. MainWindow::~MainWindow()
  12. {
  13. delete ui;
  14. }

3.2 思路说明

实现思路与参考程序1基本相同,不同点在于该程序对程序1进行了一层封装。建立一个边框管理类(MyBorderContainer),用于生成和管理边框。将特定的窗体指针传递给边框管理类进行托管即可。

3.3 优缺点说明

优点:相较于程序1,该程序只需要在窗体中创建一个边框管理类(MyBorderContainer),然后将自己的指针(this)传递给边框管理类进行托管即可。

一行代码解决问题的感觉确实很爽呢。

缺点:虽然对边框类进行了封装,但是由于边框类(MyBorder)依旧是一个独立的类,能够在其他类(除了MyBorderContainer以外)方法中创建。通常,这并不是我们想要的。

因此,我们可以再加入一点点小细节,来解决这个问题,下面请看参考程序3。

四、参考程序3

4.1源码

MyBorderContainer.h

  1. #ifndef MYBORDERCONTAINER_H
  2. #define MYBORDERCONTAINER_H
  3. #include <QWidget>
  4. #include <QObject>
  5. #include <QLabel>
  6. #include <QMouseEvent>
  7. enum BorderType
  8. {
  9. NULL_BORDER = 0, // 无
  10. L_BORDER, // 左
  11. R_BORDER, // 右
  12. T_BORDER, // 上
  13. B_BORDER, // 下
  14. LT_BORDER, // 左上
  15. LB_BORDER, //左下
  16. RT_BORDER, //右上
  17. RB_BORDER, //右下
  18. };
  19. class MyBorderContainer : public QObject
  20. {
  21. Q_OBJECT
  22. public:
  23. MyBorderContainer(QWidget *parent,uint16_t minWindowHeight = 50, uint16_t minWindowWidth = 50, uint16_t borderSize = 5);
  24. void setMinWindowSize(uint16_t minWindowWidth, uint16_t minWindowHeight); //设置窗口最小尺寸
  25. void setBorderSize(uint16_t borderSize); //设置边框捕获区域尺寸
  26. void DarwBorder(); //重新绘制边框(调整位置)
  27. protected:
  28. void InitBorder(); //初始化边框
  29. private:
  30. void getLeftScaleEvent(QPoint movPoint);
  31. void getRightScaleEvent(QPoint movPoint);
  32. void getBottomScaleEvent(QPoint movPoint);
  33. void getTopScaleEvent(QPoint movPoint);
  34. void getRBScaleEvent(QPoint movPoint);
  35. void getRTScaleEvent(QPoint movPoint);
  36. void getLTScaleEvent(QPoint movPoint);
  37. void getLBScaleEvent(QPoint movPoint);
  38. //内部边框类,防止外部创建
  39. class MyBorder:public QLabel
  40. {
  41. public:
  42. //设置边框属性
  43. MyBorder(QWidget *parent, BorderType type, MyBorderContainer * outClass);
  44. protected:
  45. void mousePressEvent(QMouseEvent *ev);
  46. void mouseMoveEvent(QMouseEvent *ev);
  47. void mouseReleaseEvent(QMouseEvent *ev);
  48. private:
  49. MyBorderContainer *container; //保存外部类指针
  50. BorderType MyType;
  51. QPoint mousePointOld;
  52. bool KeepDrag;
  53. };
  54. uint16_t borderSize; //边框捕获区大小
  55. uint16_t minWindowHeight; //最小窗口高度
  56. uint16_t minWindowWidth; //最小窗口宽度
  57. QWidget *m_widget;
  58. //边框控件
  59. MyBorder *labelLft;
  60. MyBorder *labelRit;
  61. MyBorder *labelBot;
  62. MyBorder *labelTop;
  63. MyBorder *labelRB;
  64. MyBorder *labelRT;
  65. MyBorder *labelLB;
  66. MyBorder *labelLT;
  67. };
  68. #endif // MYBORDERCONTAINER_H

MyBorder.cpp

  1. #include "MyBorderContainer.h"
  2. MyBorderContainer::MyBorder::MyBorder(QWidget *parent, BorderType type, MyBorderContainer * contex)
  3. {
  4. this->setParent(parent);
  5. KeepDrag = false;
  6. MyType = type;
  7. container = contex;
  8. if(MyType == L_BORDER || MyType == R_BORDER)
  9. {
  10. setCursor(Qt::SizeHorCursor);
  11. }
  12. else if(MyType == T_BORDER || MyType == B_BORDER)
  13. {
  14. setCursor(Qt::SizeVerCursor);
  15. }
  16. else if(MyType == LT_BORDER || MyType == RB_BORDER)
  17. {
  18. setCursor(Qt::SizeFDiagCursor);
  19. }
  20. else if(MyType == LB_BORDER || MyType == RT_BORDER)
  21. {
  22. setCursor(Qt::SizeBDiagCursor);
  23. }
  24. else
  25. {
  26. setCursor(Qt::ArrowCursor);
  27. }
  28. }
  29. //鼠标点击事件
  30. void MyBorderContainer::MyBorder::mousePressEvent(QMouseEvent *ev)
  31. {
  32. if(ev->button() == Qt::LeftButton)
  33. {
  34. KeepDrag = true;
  35. mousePointOld = ev->globalPos();
  36. }
  37. return QWidget::mousePressEvent(ev);
  38. }
  39. //鼠标移动事件
  40. void MyBorderContainer::MyBorder::mouseMoveEvent(QMouseEvent *ev)
  41. {
  42. if(KeepDrag)
  43. {
  44. const QPoint position = ev->globalPos() - mousePointOld;
  45. //调用外部类接口
  46. switch (MyType) {
  47. case L_BORDER: //左
  48. container->getLeftScaleEvent(position);
  49. break;
  50. case R_BORDER: //右
  51. container->getRightScaleEvent(position);
  52. break;
  53. case T_BORDER: //上
  54. container->getTopScaleEvent(position);
  55. break;
  56. case B_BORDER: //上
  57. container->getBottomScaleEvent(position);
  58. break;
  59. case RB_BORDER: //右下
  60. container->getRBScaleEvent(position);
  61. break;
  62. case RT_BORDER: //右上
  63. container->getRTScaleEvent(position);
  64. break;
  65. case LB_BORDER: //左下
  66. container->getLBScaleEvent(position);
  67. break;
  68. case LT_BORDER: //左上
  69. container->getLTScaleEvent(position);
  70. break;
  71. default:
  72. break;
  73. }
  74. mousePointOld = ev->globalPos();
  75. }
  76. return QWidget::mouseMoveEvent(ev);
  77. }
  78. //鼠标释放事件
  79. void MyBorderContainer::MyBorder::mouseReleaseEvent(QMouseEvent *ev)
  80. {
  81. Q_UNUSED(ev)
  82. KeepDrag = false;
  83. return QWidget::mouseReleaseEvent(ev);
  84. }

MyBorderContainer.cpp

  1. #include "MyBorderContainer.h"
  2. MyBorderContainer::MyBorderContainer(QWidget *parent,uint16_t minWindowHeight,uint16_t minWindowWidth, uint16_t borderSize)
  3. {
  4. m_widget = parent;
  5. this->minWindowHeight = minWindowHeight; //最小窗口高度
  6. this->minWindowWidth = minWindowWidth; //最小窗口宽度
  7. this->borderSize = borderSize;
  8. this->setParent(parent);
  9. InitBorder();
  10. }
  11. void MyBorderContainer::InitBorder()
  12. {
  13. //上下左右的label,为了控制界面能够拖动拉伸
  14. labelLft = new MyBorder(m_widget, L_BORDER, this);
  15. labelLft->setStyleSheet("QLabel {background-color: transparent;}");//设置背景透明
  16. labelLft->raise();
  17. labelRit = new MyBorder(m_widget, R_BORDER, this);
  18. labelRit->setStyleSheet("QLabel {background-color: transparent;}");//设置背景透明
  19. labelRit->raise();
  20. labelBot = new MyBorder(m_widget, B_BORDER, this);
  21. labelBot->setStyleSheet("QLabel {background-color: transparent;}");//设置背景透明
  22. labelBot->raise();
  23. labelTop = new MyBorder(m_widget, T_BORDER, this);
  24. labelTop->setStyleSheet("QLabel {background-color: transparent;}");//设置背景透明
  25. labelTop->raise();
  26. labelRB = new MyBorder(m_widget, RB_BORDER, this);
  27. labelRB->setStyleSheet("QLabel {background-color: transparent;}");//设置背景透明
  28. labelRB->raise();
  29. labelRT = new MyBorder(m_widget, RT_BORDER, this);
  30. labelRT->setStyleSheet("QLabel {background-color: transparent;}");//设置背景透明
  31. labelRT->raise();
  32. labelLB = new MyBorder(m_widget, LB_BORDER, this);
  33. labelLB->setStyleSheet("QLabel {background-color: transparent;}");//设置背景透明
  34. labelLB->raise();
  35. labelLT = new MyBorder(m_widget, LT_BORDER, this);
  36. labelLT->setStyleSheet("QLabel {background-color: transparent;}");//设置背景透明
  37. labelLT->raise();
  38. DarwBorder();
  39. }
  40. void MyBorderContainer::setMinWindowSize(uint16_t minWindowWidth, uint16_t minWindowHeight)
  41. {
  42. this->minWindowHeight = minWindowHeight; //最小窗口高度
  43. this->minWindowWidth = minWindowWidth; //最小窗口宽度
  44. }
  45. void MyBorderContainer::setBorderSize(uint16_t borderSize)
  46. {
  47. this->borderSize = borderSize;
  48. }
  49. //重绘边框
  50. void MyBorderContainer::DarwBorder()
  51. {
  52. labelLft->setGeometry(0,0,this->borderSize,m_widget->height());
  53. labelRit->setGeometry(m_widget->width()-this->borderSize,0,this->borderSize,m_widget->height());
  54. labelBot->setGeometry(0,m_widget->height()-this->borderSize,m_widget->width(),this->borderSize);
  55. labelTop->setGeometry(0,0,m_widget->width(),this->borderSize);
  56. labelRB->setGeometry(m_widget->width()-this->borderSize-1,m_widget->height()-this->borderSize-1,m_widget->width(),m_widget->height());
  57. labelRT->setGeometry(m_widget->width()-this->borderSize-1,0,m_widget->width(),this->borderSize+1);
  58. labelLB->setGeometry(0,m_widget->height()-this->borderSize-1,this->borderSize+1,m_widget->width());
  59. labelLT->setGeometry(0,0,this->borderSize+1,this->borderSize+1);
  60. }
  61. void MyBorderContainer::getLeftScaleEvent(QPoint movPoint)
  62. {
  63. if((m_widget->pos().x()+movPoint.x())>(m_widget->pos().x()+m_widget->width()-minWindowWidth))
  64. {
  65. return;//保证拖动窗口左边界的时候,控件宽度至少有200
  66. }
  67. m_widget->setGeometry(m_widget->pos().x()+movPoint.x(),m_widget->pos().y(),m_widget->width()-movPoint.x(),m_widget->height());
  68. DarwBorder();
  69. }
  70. void MyBorderContainer::getRightScaleEvent(QPoint movPoint)
  71. {
  72. if((m_widget->pos().x()+m_widget->width()+movPoint.x())<(m_widget->pos().x()+minWindowWidth))
  73. {
  74. return;//保证拖动窗口右边界的时候,控件宽度至少有200
  75. }
  76. m_widget->setGeometry(m_widget->pos().x(),m_widget->pos().y(),m_widget->width()+movPoint.x(),m_widget->height());
  77. DarwBorder();
  78. }
  79. void MyBorderContainer::getBottomScaleEvent(QPoint movPoint)
  80. {
  81. if((m_widget->pos().y()+m_widget->height()+movPoint.y())<(m_widget->pos().y()+minWindowHeight))
  82. {
  83. return;//保证拖动窗口下边界的时候,控件高度至少有200
  84. }
  85. m_widget->setGeometry(m_widget->pos().x(),m_widget->pos().y(),m_widget->width(),m_widget->height()+movPoint.y());
  86. DarwBorder();
  87. }
  88. void MyBorderContainer::getTopScaleEvent(QPoint movPoint)
  89. {
  90. if((m_widget->pos().y()+movPoint.y())>(m_widget->pos().y()+m_widget->height()-minWindowHeight))
  91. {
  92. return;//保证拖动窗口上边界的时候,控件高度至少有200
  93. }
  94. m_widget->setGeometry(m_widget->pos().x(),m_widget->pos().y()+movPoint.y(),m_widget->width(),m_widget->height()-movPoint.y());
  95. DarwBorder();
  96. }
  97. void MyBorderContainer::getRBScaleEvent(QPoint movPoint)
  98. {
  99. if((m_widget->pos().y()+m_widget->height()+movPoint.y())<(m_widget->pos().y()+minWindowHeight)
  100. || (m_widget->pos().x()+m_widget->width()+movPoint.x())<(m_widget->pos().x()+minWindowWidth))
  101. {
  102. return;//保证拖动窗口上边界的时候,控件高度和宽度至少有200
  103. }
  104. m_widget->setGeometry(m_widget->pos().x(),m_widget->pos().y(),m_widget->width()+movPoint.x(),m_widget->height());
  105. m_widget->setGeometry(m_widget->pos().x(),m_widget->pos().y(),m_widget->width(),m_widget->height()+movPoint.y());
  106. DarwBorder();
  107. }
  108. void MyBorderContainer::getRTScaleEvent(QPoint movPoint)
  109. {
  110. if((m_widget->pos().x()+m_widget->width()+movPoint.x())<(m_widget->pos().x()+minWindowWidth)
  111. || (m_widget->pos().y()+movPoint.y())>(m_widget->pos().y()+m_widget->height()-minWindowHeight) )
  112. {
  113. return;//保证拖动窗口上边界的时候,控件高度和宽度至少有200
  114. }
  115. m_widget->setGeometry(m_widget->pos().x(),m_widget->pos().y()+movPoint.y(),m_widget->width(),m_widget->height()-movPoint.y());
  116. m_widget->setGeometry(m_widget->pos().x(),m_widget->pos().y(),m_widget->width()+movPoint.x(),m_widget->height());
  117. DarwBorder();
  118. }
  119. void MyBorderContainer::getLTScaleEvent(QPoint movPoint)
  120. {
  121. if((m_widget->pos().x()+movPoint.x())>(m_widget->pos().x()+m_widget->width()-minWindowWidth)
  122. || (m_widget->pos().y()+movPoint.y())>(m_widget->pos().y()+m_widget->height()-minWindowHeight) )
  123. {
  124. return;//保证拖动窗口上边界的时候,控件高度和宽度至少有200
  125. }
  126. m_widget->setGeometry(m_widget->pos().x()+movPoint.x(),m_widget->pos().y(),m_widget->width()-movPoint.x(),m_widget->height());
  127. m_widget->setGeometry(m_widget->pos().x(),m_widget->pos().y()+movPoint.y(),m_widget->width(),m_widget->height()-movPoint.y());
  128. DarwBorder();
  129. }
  130. void MyBorderContainer::getLBScaleEvent(QPoint movPoint)
  131. {
  132. if((m_widget->pos().x()+movPoint.x())>(m_widget->pos().x()+m_widget->width()-minWindowWidth)
  133. || (m_widget->pos().y()+m_widget->height()+movPoint.y())<(m_widget->pos().y()+minWindowHeight))
  134. {
  135. return;//保证拖动窗口上边界的时候,控件高度和宽度至少有200
  136. }
  137. m_widget->setGeometry(m_widget->pos().x()+movPoint.x(),m_widget->pos().y(),m_widget->width()-movPoint.x(),m_widget->height());
  138. m_widget->setGeometry(m_widget->pos().x(),m_widget->pos().y(),m_widget->width(),m_widget->height()+movPoint.y());
  139. DarwBorder();
  140. }

4.2 思路说明

  该程序将边框类(MyBorder)设置为边框管理类(MyBorderContainer)的内部类,这样就可以让外部无法创建MyBorder对象。

4.3 优缺点说明

 参考程序3进一步加深了程序的封装程度,将MyBorder.h和MyBorderContainer.h以及MyBorderContainer.cpp三个文件打包后,就可以当做一个模块来使用了,非常方便,当然封装有利有弊,应该根据实际需要进行选择。

4.4 GitHub

  目前以及将参考程序3打包成模块,并编写了简单的Demo上传至GitHub上

  链接:BorderDemo

  将其中的BorderDemo.pro文件导入Qt Creator并选择自己的编译器即可使用。

五、结语

  不论你选择以上3种程序的哪种方案,亦或者网上的其他方案,都不重要。相比于纠结哪种实现过程,实现的思路更为重要。编程时,多思考因为-所以才是最快乐的。

  什么?你还想要窗体可以拖动?那当然是封装一个标题栏啦!(待更新)

  如果你有什么不懂的地方,亦或者发现博主某个地方存在错误,欢迎留言交流。感谢你的反馈。

  如果你觉得这篇文章对你有帮助,请在文章的左下角点个赞。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/94958
推荐阅读
相关标签
  

闽ICP备14008679号