关于移动无边窗体

想法一:

void EditDialog::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        m_Drag = true;
        m_DragPosition = event->globalPos() - this->pos();
        event->accept();
    }
}

void EditDialog::mouseMoveEvent(QMouseEvent *event)
{
    if (m_Drag && (event->buttons() && Qt::LeftButton)) {
        move(event->globalPos() - m_DragPosition);
        event->accept();
    }
}

void EditDialog::mouseReleaseEvent(QMouseEvent *)
{
    m_Drag = false;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

如果不考虑跨平台,只针对Windows平台,那么我建议用Windows下的标准方法,模拟标题栏移动消息,既简单又高效。
想法二:

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if (ReleaseCapture())
        SendMessage(HWND(this->winId()), WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
    event->ignore();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

这样,在拖动窗体时只会在松开鼠标时才将窗体移动过去,这样就避免了第一种方法的低效率问题。


猜你喜欢

转载自blog.csdn.net/hutianyang2010/article/details/80791506
今日推荐