QT自定義標題欄之拖動窗體
1,定義兩個成員變數
bool m_pressed;
QPoint m_movePos;
2,重寫mousePressed,mouseMove,mouseRelease
void mousePressEvent(QMouseEvent *event)
{
m_pressed = true;
m_movePos = event->globalPos() - pos();
return QMainWindow::mousePressEvent(event);
}
void mouseMoveEvent(QMouseEvent *event)
{
if (m_pressed
&& (event->buttons()&& Qt::LeftButton)
&& (event->globalPos() - m_movePos).manhattanLength() > QApplication::startDragDistance())
{
QPoint movePos = event->globalPos() - m_movePos;
this->move(movePos);
m_movePos = event->globalPos() - pos();
}
return QMainWindow::mouseMoveEvent(event);
}
void mouseReleaseEvent(QMouseEvent *event)
{
m_pressed = false;
return QMainWindow::mouseReleaseEvent(event);
}