#include SignalWin::SignalWin(QWidget *parent) : QWidget(parent), m_bPressed(false), m_bSizeChanging(false){ ui.setupUi(this);}SignalWin::~Signa..._前台鼠标拉伸">
当前位置:   article > 正文

无边框窗口鼠标拉伸_前台鼠标拉伸

前台鼠标拉伸

 窗口区域划分

  1. #include "SignalWin.h"
  2. #include "Mycanvas.h"
  3. #include <QDebug>
  4. #include <QMouseEvent>
  5. SignalWin::SignalWin(QWidget *parent)
  6. : QWidget(parent),
  7. m_bPressed(false),
  8. m_bSizeChanging(false)
  9. {
  10. ui.setupUi(this);
  11. }
  12. SignalWin::~SignalWin()
  13. {
  14. }
  15. void SignalWin::setCurWinInfo(SignalWinInfo info)
  16. {
  17. m_info = info;
  18. setMouseTracking(true); //开启鼠标位置追踪
  19. ui.WinFrame->setMouseTracking(true); //开启鼠标位置追踪
  20. ui.SignalWinFrame->setMouseTracking(true);
  21. }
  22. void SignalWin::mousePressEvent(QMouseEvent *event)
  23. {
  24. m_startPos = event->pos();
  25. m_startWPos = pos();
  26. pLast = event->globalPos();
  27. m_bPressed = true;
  28. m_wid = geometry();
  29. raise();
  30. }
  31. int poss = 0;
  32. void SignalWin::mouseMoveEvent(QMouseEvent *event)
  33. {
  34. if (this->isMaximized())
  35. return;
  36. if (!m_bSizeChanging)
  37. {
  38. poss = countFlag(event->pos(), countRow(event->pos()));//计算出来鼠标在哪个区域
  39. if (!event->buttons())
  40. setCursorType(poss);//设置鼠标形状
  41. }
  42. if ((event->buttons() == Qt::LeftButton) && m_bPressed)
  43. {
  44. m_bSizeChanging = true;
  45. QPoint ptemp;
  46. QPoint Framepos = event->globalPos();
  47. ptemp = m_startWPos + Framepos - pLast; //鼠标移动的偏移量
  48. if (poss == 22) //区域(2,2)表示移动窗口
  49. {
  50. move(ptemp);
  51. }
  52. else
  53. {
  54. QRect wid = geometry();
  55. int minWidth = this->minimumWidth();
  56. int minHeight = this->minimumHeight();
  57. switch (poss)//改变窗口的大小
  58. {
  59. case 11:
  60. {
  61. QPoint pos = m_wid.topLeft();
  62. pos.rx() = pos.rx() + Framepos.x() - pLast.x();
  63. pos.ry() = pos.ry() + Framepos.y() - pLast.y();
  64. wid.setTopLeft(pos);
  65. break;//左上角
  66. }
  67. case 12:
  68. {
  69. int topY = m_wid.top();
  70. topY = topY + Framepos.y() - pLast.y();
  71. wid.setTop(topY);
  72. break;//中上角
  73. }
  74. case 13:
  75. {
  76. QPoint pos = m_wid.topRight();
  77. pos.rx() = pos.rx() + Framepos.x() - pLast.x();
  78. pos.ry() = pos.ry() + Framepos.y() - pLast.y();
  79. wid.setTopRight(pos);
  80. break;//右上角
  81. }
  82. case 21:
  83. {
  84. int leftX= m_wid.left();
  85. leftX = leftX + Framepos.x() - pLast.x();
  86. wid.setLeft(leftX);
  87. break;//中左角
  88. }
  89. case 23:
  90. {
  91. int rightX = m_wid.right();
  92. rightX = rightX + Framepos.x() - pLast.x();
  93. wid.setRight(rightX);
  94. break;//中右角
  95. }
  96. case 31:
  97. {
  98. QPoint pos = m_wid.bottomLeft();
  99. pos.rx() = pos.rx() + Framepos.x() - pLast.x();
  100. pos.ry() = pos.ry() + Framepos.y() - pLast.y();
  101. wid.setBottomLeft(pos);
  102. break;//左下角
  103. }
  104. case 32:
  105. {
  106. int bottomY = m_wid.bottom();
  107. bottomY = bottomY + Framepos.y() - pLast.y();
  108. wid.setBottom(bottomY);
  109. break;//中下角
  110. }
  111. case 33:
  112. {
  113. QPoint pos = m_wid.bottomRight();
  114. pos.rx() = pos.rx() + Framepos.x() - pLast.x();
  115. pos.ry() = pos.ry() + Framepos.y() - pLast.y();
  116. wid.setBottomRight(pos);
  117. break;//左下角
  118. }
  119. }
  120. setGeometry(wid); //设置窗口的位置
  121. }
  122. }
  123. }
  124. void SignalWin::mouseReleaseEvent(QMouseEvent *event)
  125. {
  126. m_bPressed = false;
  127. m_bSizeChanging = false;
  128. }
  129. int SignalWin::countFlag(QPoint p, int row)//计算鼠标在哪一列和哪一行
  130. {
  131. if (p.y() < MARGIN)
  132. return 10 + row;
  133. else if (p.y() > this->height() - MARGIN)
  134. return 30 + row;
  135. else
  136. return 20 + row;
  137. }
  138. void SignalWin::setCursorType(int flag)//根据鼠标所在位置改变鼠标指针形状
  139. {
  140. Qt::CursorShape cursor;
  141. switch (flag)
  142. {
  143. case 11:
  144. case 33:
  145. cursor = Qt::SizeFDiagCursor; break;
  146. case 13:
  147. case 31:
  148. cursor = Qt::SizeBDiagCursor; break;
  149. case 21:
  150. case 23:
  151. cursor = Qt::SizeHorCursor; break;
  152. case 12:
  153. case 32:
  154. cursor = Qt::SizeVerCursor; break;
  155. case 22:
  156. cursor = Qt::ArrowCursor; break;
  157. default:
  158. QApplication::restoreOverrideCursor();//恢复鼠标指针性状
  159. break;
  160. }
  161. setCursor(cursor);
  162. }
  163. int SignalWin::countRow(QPoint p)
  164. {
  165. int row = 0;
  166. if (p.x() < MARGIN)
  167. return row + 1;
  168. else if (p.x() > this->width() - MARGIN)
  169. return row + 3;
  170. else
  171. return 2 + row;
  172. }

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/94968
推荐阅读
相关标签