Qt窗口拖動及改變大小

  1.    
  1. #ifndef MYDRAGQT_H  
  2. #define MYDRAGQT_H  
  3.   
  4. #include <QtWidgets/QWidget>  
  5. #include "ui_mydragqt.h"  
  6.   
  7. class MyDragQt : public QWidget  
  8. {  
  9.     Q_OBJECT  
  10.   
  11. public:  
  12.     MyDragQt(QWidget *parent = 0);  
  13.     ~MyDragQt();  
  14.   
  15. private:  
  16.     Ui::MyDragQtClass ui;  
  17. private:  
  18.     void paintEvent(QPaintEvent*);  
  19.     void mousePressEvent (QMouseEvent* event);  
  20.     void mouseReleaseEvent(QMouseEvent* event);  
  21.     void mouseMoveEvent(QMouseEvent* event);  
  22. private:  
  23.     //輔助函數  
  24.     int calcPosition(const QPoint& pt);  
  25.     void setCursorType(int value);  
  26. private:  
  27.     bool                    m_bLeftMouseButtonPressed;  
  28.     int                     m_lastPosition;  
  29.     QPoint                  m_ptLast;  
  30.     Qt::CursorShape         m_currentCursor;  
  31. };  
  32.   
  33. #endif // MYDRAGQT_H  


實現:

  1. #include "mydragqt.h"  
  2.   
  3. #include <QPainter>  
  4. #include <QMouseEvent>  
  5. #include <QDebug>  
  6. namespace  
  7. {  
  8.     const int g_padding = 4;  
  9.   
  10.     int helperCalcPosition(int pos, int range)  
  11.     {  
  12.         return (pos < g_padding) ? 0 : ((pos > (range - g_padding)) ? 2 : 1);  
  13.     }  
  14. }  
  15.   
  16.   
  17. MyDragQt::MyDragQt(QWidget *parent)  
  18.     : QWidget(parent, Qt::FramelessWindowHint)  
  19.     , m_bLeftMouseButtonPressed(false)  
  20.     , m_lastPosition(11)  
  21.     , m_currentCursor(Qt::ArrowCursor)  
  22. {  
  23.     ui.setupUi(this);  
  24.     setMouseTracking(true);  
  25.     setCursor(m_currentCursor);  
  26.   
  27.     this->setMinimumSize(QSize(280, 360));  
  28. }  
  29.   
  30. MyDragQt::~MyDragQt()  
  31. {  
  32.   
  33. }  
  34.   
  35. void MyDragQt::paintEvent(QPaintEvent*)  
  36. {  
  37.     QPainter p(this);  
  38.     p.fillRect(rect(), Qt::black);  
  39. }  
  40.   
  41. int MyDragQt::calcPosition(const QPoint& pt)  
  42. {  
  43.     int row = helperCalcPosition(pt.y(), rect().height());  
  44.     int col = helperCalcPosition(pt.x(), rect().width());  
  45.     return row * 10 + col;  
  46. }  
  47.   
  48. void MyDragQt::mousePressEvent(QMouseEvent* event)  
  49. {  
  50.     m_bLeftMouseButtonPressed = true;  
  51.     m_ptLast = event->globalPos();  
  52.     m_lastPosition = calcPosition(event->pos());  
  53.     event->ignore();  
  54. }  
  55.   
  56. void MyDragQt::mouseReleaseEvent(QMouseEvent* event)  
  57. {  
  58.     if(m_bLeftMouseButtonPressed) m_bLeftMouseButtonPressed = false;  
  59.     event->ignore();  
  60. }  
  61.   
  62. void MyDragQt::mouseMoveEvent(QMouseEvent* event)  
  63. {  
  64.     setCursorType(calcPosition(event->pos()));  
  65.     if(m_bLeftMouseButtonPressed)  
  66.     {  
  67.         QPoint ptNew = event->globalPos();  
  68.         ptNew -= m_ptLast;  
  69.         if(11 == m_lastPosition) //拖動  
  70.         {  
  71.             ptNew += pos();  
  72.             move(ptNew);  
  73.         }  
  74.         else //調整大小   
  75.         {  
  76.             QRect rectWindow = geometry();  
  77.             switch(m_lastPosition)  
  78.             {  
  79.             case 00:      
  80.                 rectWindow.setTopLeft(rectWindow.topLeft() + ptNew);  
  81.                 break;  
  82.             case 02:  
  83.                 rectWindow.setTopRight(rectWindow.topRight() + ptNew);  
  84.                 break;  
  85.             case 20:  
  86.                 rectWindow.setBottomLeft(rectWindow.bottomLeft() + ptNew);  
  87.                 break;  
  88.             case 22:  
  89.                 rectWindow.setBottomRight(rectWindow.bottomRight() + ptNew);  
  90.                 break;  
  91.             case 10:  
  92.                 rectWindow.setLeft(rectWindow.left() + ptNew.x());  
  93.                 break;  
  94.             case 12:  
  95.                 rectWindow.setRight(rectWindow.right() + ptNew.x());  
  96.                 break;  
  97.             case 01:  
  98.                 rectWindow.setTop(rectWindow.top() + ptNew.y());  
  99.                 break;  
  100.             case 21:  
  101.                 rectWindow.setBottom(rectWindow.bottom() + ptNew.y());  
  102.                 break;  
  103.             //case 11:  
  104.             default:  
  105.                 Q_ASSERT(0);  
  106.             }  
  107.             setGeometry(rectWindow);  
  108.             //qDebug() << rectWindow.topLeft() << ", " << rectWindow.bottomRight();  
  109.         }  
  110.         m_ptLast  = event->globalPos();  
  111.     }  
  112.     event->ignore();  
  113. }  
  114.   
  115. void MyDragQt::setCursorType(int value)  
  116. {  
  117.     Qt::CursorShape cursor;  
  118.   
  119.     switch(value)  
  120.     {  
  121.     case 00:  
  122.     case 22:  
  123.         cursor = Qt::SizeFDiagCursor;  
  124.         break;  
  125.     case 02:  
  126.     case 20:  
  127.         cursor = Qt::SizeBDiagCursor;  
  128.         break;  
  129.     case 10:  
  130.     case 12:  
  131.         cursor = Qt::SizeHorCursor;  
  132.         break;  
  133.     case 01:  
  134.     case 21:  
  135.         cursor = Qt::SizeVerCursor;  
  136.         break;  
  137.     case 11:  
  138.         cursor = Qt::ArrowCursor;  
  139.         break;  
  140.     default:  
  141.         Q_ASSERT(0);  
  142.         break;  
  143.     }  
  144.     if(cursor != m_currentCursor)  
  145.     {  
  146.         m_currentCursor = cursor;  
  147.         setCursor(m_currentCursor);  
  148.     }  
  149. }  

這個實現比大多數的要精簡。下一步繼續優化,爭取再減少一個成員變量。

發佈了3 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章