admin管理员组文章数量:1662355
Win7系统不得不说是非常好用的,也是目前为止占用份额最大的操作系统,其中win7有个效果,将窗体拖动到顶部时会自动最大化,拖动到左侧右侧时会自动半屏显示,再次拖动窗体到其他位置,会重新恢复之前的大小,这个效果还是比较人性化的,大大方便了很多用户的操作习惯。
在Qt中,如果是无边框窗体,(有边框窗体和操作系统窗体效果一致)并没有相关的API接口来实现这个效果,必须自己写代码来模拟这个效果,原理很简单,绑定事件过滤器,自动计算当前无边框窗体的位置和鼠标按下去的坐标,当到达顶部或者左侧右侧时,自动设置该窗体的geometry即可。
为了复用代码,我这里绑定的全局事件过滤器,这样只需要无边框窗体界面设置两行即可,无需重复编码。
无边框窗体代码:
this->setProperty("canMove", true);
this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
核心代码:
#include "appinit.h"
#include "qapplication.h"
#include "qdesktopwidget.h"
#include "qevent.h"
#include "qwidget.h"
#include "qdebug.h"
AppInit *AppInit::self = 0;
AppInit::AppInit(QObject *parent) : QObject(parent)
{
}
bool AppInit::eventFilter(QObject *obj, QEvent *evt)
{
QWidget *w = (QWidget *)obj;
if (!w->property("canMove").toBool()) {
return QObject::eventFilter(obj, evt);
}
//存储桌面宽高以及全屏/左侧半屏/右侧半屏 区域
static int deskWidth = qApp->desktop()->availableGeometry().width();
static int deskHeight = qApp->desktop()->availableGeometry().height();
static QRect fullRect = qApp->desktop()->availableGeometry();
static QRect leftRect = QRect(0, 0, deskWidth / 2, deskHeight);
static QRect rightRect = QRect(deskWidth / 2, 0, deskWidth / 2, deskHeight);
bool autoRect = w->property("autoRect").toBool();
static QPoint mousePoint;
static bool mousePressed = false;
QMouseEvent *event = static_cast<QMouseEvent *>(evt);
if (event->type() == QEvent::MouseButtonPress) {
if (event->button() == Qt::LeftButton) {
mousePressed = true;
mousePoint = event->globalPos() - w->pos();
return true;
}
} else if (event->type() == QEvent::MouseButtonRelease) {
mousePressed = false;
//计算全局坐标
int x = event->globalPos().x();
int y = event->globalPos().y();
int offset = 10;
//如果Y坐标在桌面顶部,则自动最大化
//如果X坐标在桌面左侧,则自动左侧半屏幕
//如果X坐标在桌面右侧,则自动右侧半屏幕
//自动变化后记住当前窗体是自动产生的位置,以便下次恢复时自动应用变化前的位置
if (!autoRect) {
//存储最后一次的位置,自动矫正负数的坐标
int oldX = w->geometry().x();
oldX = oldX < 0 ? 0 : oldX;
int oldY = w->geometry().y();
oldY = oldY < 0 ? 0 : oldY;
QRect oldRect = QRect(oldX, oldY, w->geometry().width(), w->geometry().height());
if (y < offset) {
w->setProperty("autoRect", true);
w->setProperty("oldRect", oldRect);
w->setGeometry(fullRect);
} else if (x < offset) {
w->setProperty("autoRect", true);
w->setProperty("oldRect", oldRect);
w->setGeometry(leftRect);
} else if (x > (deskWidth - offset)) {
w->setProperty("autoRect", true);
w->setProperty("oldRect", oldRect);
w->setGeometry(rightRect);
}
}
return true;
} else if (event->type() == QEvent::MouseMove) {
if (mousePressed && (event->buttons() && Qt::LeftButton)) {
if (!autoRect) {
w->move(event->globalPos() - mousePoint);
} else {
QRect oldRect = w->property("oldRect").toRect();
w->setProperty("autoRect", false);
w->setGeometry(oldRect);
}
return true;
}
}
return QObject::eventFilter(obj, evt);
}
void AppInit::start()
{
qApp->installEventFilter(this);
}
版权声明:本文标题:Qt仿win7自动顶部最大化左侧右侧半屏效果 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1729960115a1217512.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论