QT之窗口移动
应用背景
我们为什么需要移动窗口,在我们设计界面上,我们不需要QT自带的默认边框。我们要隐藏它,但是隐藏后,你却发现我的窗口不能移动了。这样你懂的!QT之窗口移动就诞生了。嘿嘿~
窗口移动效果图
闲扯两句
其实要做到这个效果不是很难的,但是,我还是相信有需要的小伙伴会需要。我还是简单的写上了本篇文章,希望能帮助到需要的小伙伴。
上代码
void Demo::mouseMoveEvent(QMouseEvent*event)
{
if (event->buttons() & Qt::LeftButton)
{
QRect desktopRc = QApplication::desktop()->availableGeometry();
QPoint curPoint = event->globalPos() - m_dragPos;
if (event->globalY() > desktopRc.height())
{
curPoint.setY(desktopRc.height() - m_dragPos.y());
}
move(curPoint);
}
QWidget::mouseMoveEvent(event);
}
void Demo::mousePressEvent(QMouseEvent*event)
{
if (event->button() == Qt::LeftButton)
{
m_dragPos = QCursor::pos() - frameGeometry().topLeft();
}
QWidget::mousePressEvent(event);
}
//是不是很简单呐,你也只需要复制这两段代码,你也可以去实现你的窗口移动啦!
结尾
只为记录,只为分享! 愿所写能对你有所帮助。Good Good Study, Day Day Up!