- #ifndef MYDRAGQT_H
- #define MYDRAGQT_H
- #include <QtWidgets/QWidget>
- #include "ui_mydragqt.h"
- class MyDragQt : public QWidget
- {
- Q_OBJECT
- public:
- MyDragQt(QWidget *parent = 0);
- ~MyDragQt();
- private:
- Ui::MyDragQtClass ui;
- private:
- void paintEvent(QPaintEvent*);
- void mousePressEvent (QMouseEvent* event);
- void mouseReleaseEvent(QMouseEvent* event);
- void mouseMoveEvent(QMouseEvent* event);
- private:
- //辅助函数
- int calcPosition(const QPoint& pt);
- void setCursorType(int value);
- private:
- bool m_bLeftMouseButtonPressed;
- int m_lastPosition;
- QPoint m_ptLast;
- Qt::CursorShape m_currentCursor;
- };
- #endif // MYDRAGQT_H
实现:
- #include "mydragqt.h"
- #include <QPainter>
- #include <QMouseEvent>
- #include <QDebug>
- namespace
- {
- const int g_padding = 4;
- int helperCalcPosition(int pos, int range)
- {
- return (pos < g_padding) ? 0 : ((pos > (range - g_padding)) ? 2 : 1);
- }
- }
- MyDragQt::MyDragQt(QWidget *parent)
- : QWidget(parent, Qt::FramelessWindowHint)
- , m_bLeftMouseButtonPressed(false)
- , m_lastPosition(11)
- , m_currentCursor(Qt::ArrowCursor)
- {
- ui.setupUi(this);
- setMouseTracking(true);
- setCursor(m_currentCursor);
- this->setMinimumSize(QSize(280, 360));
- }
- MyDragQt::~MyDragQt()
- {
- }
- void MyDragQt::paintEvent(QPaintEvent*)
- {
- QPainter p(this);
- p.fillRect(rect(), Qt::black);
- }
- int MyDragQt::calcPosition(const QPoint& pt)
- {
- int row = helperCalcPosition(pt.y(), rect().height());
- int col = helperCalcPosition(pt.x(), rect().width());
- return row * 10 + col;
- }
- void MyDragQt::mousePressEvent(QMouseEvent* event)
- {
- m_bLeftMouseButtonPressed = true;
- m_ptLast = event->globalPos();
- m_lastPosition = calcPosition(event->pos());
- event->ignore();
- }
- void MyDragQt::mouseReleaseEvent(QMouseEvent* event)
- {
- if(m_bLeftMouseButtonPressed) m_bLeftMouseButtonPressed = false;
- event->ignore();
- }
- void MyDragQt::mouseMoveEvent(QMouseEvent* event)
- {
- setCursorType(calcPosition(event->pos()));
- if(m_bLeftMouseButtonPressed)
- {
- QPoint ptNew = event->globalPos();
- ptNew -= m_ptLast;
- if(11 == m_lastPosition) //拖动
- {
- ptNew += pos();
- move(ptNew);
- }
- else //调整大小
- {
- QRect rectWindow = geometry();
- switch(m_lastPosition)
- {
- case 00:
- rectWindow.setTopLeft(rectWindow.topLeft() + ptNew);
- break;
- case 02:
- rectWindow.setTopRight(rectWindow.topRight() + ptNew);
- break;
- case 20:
- rectWindow.setBottomLeft(rectWindow.bottomLeft() + ptNew);
- break;
- case 22:
- rectWindow.setBottomRight(rectWindow.bottomRight() + ptNew);
- break;
- case 10:
- rectWindow.setLeft(rectWindow.left() + ptNew.x());
- break;
- case 12:
- rectWindow.setRight(rectWindow.right() + ptNew.x());
- break;
- case 01:
- rectWindow.setTop(rectWindow.top() + ptNew.y());
- break;
- case 21:
- rectWindow.setBottom(rectWindow.bottom() + ptNew.y());
- break;
- //case 11:
- default:
- Q_ASSERT(0);
- }
- setGeometry(rectWindow);
- //qDebug() << rectWindow.topLeft() << ", " << rectWindow.bottomRight();
- }
- m_ptLast = event->globalPos();
- }
- event->ignore();
- }
- void MyDragQt::setCursorType(int value)
- {
- Qt::CursorShape cursor;
- switch(value)
- {
- case 00:
- case 22:
- cursor = Qt::SizeFDiagCursor;
- break;
- case 02:
- case 20:
- cursor = Qt::SizeBDiagCursor;
- break;
- case 10:
- case 12:
- cursor = Qt::SizeHorCursor;
- break;
- case 01:
- case 21:
- cursor = Qt::SizeVerCursor;
- break;
- case 11:
- cursor = Qt::ArrowCursor;
- break;
- default:
- Q_ASSERT(0);
- break;
- }
- if(cursor != m_currentCursor)
- {
- m_currentCursor = cursor;
- setCursor(m_currentCursor);
- }
- }
这个实现比大多数的要精简。下一步继续优化,争取再减少一个成员变量。