Qt如何实现通用无边框拖动拉伸
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,这篇文章主要介绍"Qt如何实现通用无边框拖动拉伸",在日常操作中,相信很多人在Qt如何实现通用无边框拖动拉伸问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Qt如何实现通
千家信息网最后更新 2026年11月01日Qt如何实现通用无边框拖动拉伸
这篇文章主要介绍"Qt如何实现通用无边框拖动拉伸",在日常操作中,相信很多人在Qt如何实现通用无边框拖动拉伸问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Qt如何实现通用无边框拖动拉伸"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
一、前言
相信各位CS结构开发的程序员,多多少少都遇到过需要美化界面的事情,一般都不会采用系统的标题栏,这样就需要无边框标题栏窗体,默认的话无边框的标题栏都不支持拉伸和拖动的,毕竟去掉了标题栏则意味着失去了系统的窗体的属性,拉伸和拖动都需要自己写代码去实现,网上有很多类似的开源的方案,我也看过不少,总体来说复杂了些,对于初学者来说有可能看的云里雾里的,比如边框四周八个方位都可以自由拉伸这块,我的思路是针对设定的八个方位的区域进行识别鼠标是否按下,按下的哪个部位则执行什么拉伸策略,鼠标移到哪个位置则对应改变鼠标指针形状,更浅显易懂一些,至于拖动移动,还可以设置拖动的标题栏的高度等。
主要功能:
可以指定需要无边框的widget
边框四周八个方位都可以自由拉伸
可设置对应位置的边距,以便识别更大区域
可设置是否允许拖动
可设置是否允许拉伸
二、代码思路
bool FramelessWidget::eventFilter(QObject *watched, QEvent *event){ if (widget != 0 && watched == widget) { if (event->type() == QEvent::Resize) { //重新计算八个描点的区域,描点区域的作用还有就是计算鼠标坐标是否在某一个区域内 int width = widget->width(); int height = widget->height(); //左侧描点区域 rectLeft = QRect(0, padding, padding, height - padding * 2); //上侧描点区域 rectTop = QRect(padding, 0, width - padding * 2, padding); //右侧描点区域 rectRight = QRect(width - padding, padding, padding, height - padding * 2); //下侧描点区域 rectBottom = QRect(padding, height - padding, width - padding * 2, padding); //左上角描点区域 rectLeftTop = QRect(0, 0, padding, padding); //右上角描点区域 rectRightTop = QRect(width - padding, 0, padding, padding); //左下角描点区域 rectLeftBottom = QRect(0, height - padding, padding, padding); //右下角描点区域 rectRightBottom = QRect(width - padding, height - padding, padding, padding); } else if (event->type() == QEvent::HoverMove) { //设置对应鼠标形状,这个必须放在这里而不是下面,因为可以在鼠标没有按下的时候识别 QHoverEvent *hoverEvent = (QHoverEvent *)event; QPoint point = hoverEvent->pos(); if (resizeEnable) { if (rectLeft.contains(point)) { widget->setCursor(Qt::SizeHorCursor); } else if (rectRight.contains(point)) { widget->setCursor(Qt::SizeHorCursor); } else if (rectTop.contains(point)) { widget->setCursor(Qt::SizeVerCursor); } else if (rectBottom.contains(point)) { widget->setCursor(Qt::SizeVerCursor); } else if (rectLeftTop.contains(point)) { widget->setCursor(Qt::SizeFDiagCursor); } else if (rectRightTop.contains(point)) { widget->setCursor(Qt::SizeBDiagCursor); } else if (rectLeftBottom.contains(point)) { widget->setCursor(Qt::SizeBDiagCursor); } else if (rectRightBottom.contains(point)) { widget->setCursor(Qt::SizeFDiagCursor); } else { widget->setCursor(Qt::ArrowCursor); } } //根据当前鼠标位置,计算XY轴移动了多少 int offsetX = point.x() - lastPos.x(); int offsetY = point.y() - lastPos.y(); //根据按下处的位置判断是否是移动控件还是拉伸控件 if (moveEnable) { if (pressed) { widget->move(widget->x() + offsetX, widget->y() + offsetY); } } if (resizeEnable) { if (pressedLeft) { int resizeW = widget->width() - offsetX; if (widget->minimumWidth() <= resizeW) { widget->setGeometry(widget->x() + offsetX, rectY, resizeW, rectH); } } else if (pressedRight) { widget->setGeometry(rectX, rectY, rectW + offsetX, rectH); } else if (pressedTop) { int resizeH = widget->height() - offsetY; if (widget->minimumHeight() <= resizeH) { widget->setGeometry(rectX, widget->y() + offsetY, rectW, resizeH); } } else if (pressedBottom) { widget->setGeometry(rectX, rectY, rectW, rectH + offsetY); } else if (pressedLeftTop) { int resizeW = widget->width() - offsetX; int resizeH = widget->height() - offsetY; if (widget->minimumWidth() <= resizeW) { widget->setGeometry(widget->x() + offsetX, widget->y(), resizeW, resizeH); } if (widget->minimumHeight() <= resizeH) { widget->setGeometry(widget->x(), widget->y() + offsetY, resizeW, resizeH); } } else if (pressedRightTop) { int resizeW = rectW + offsetX; int resizeH = widget->height() - offsetY; if (widget->minimumHeight() <= resizeH) { widget->setGeometry(widget->x(), widget->y() + offsetY, resizeW, resizeH); } } else if (pressedLeftBottom) { int resizeW = widget->width() - offsetX; int resizeH = rectH + offsetY; if (widget->minimumWidth() <= resizeW) { widget->setGeometry(widget->x() + offsetX, widget->y(), resizeW, resizeH); } if (widget->minimumHeight() <= resizeH) { widget->setGeometry(widget->x(), widget->y(), resizeW, resizeH); } } else if (pressedRightBottom) { int resizeW = rectW + offsetX; int resizeH = rectH + offsetY; widget->setGeometry(widget->x(), widget->y(), resizeW, resizeH); } } } else if (event->type() == QEvent::MouseButtonPress) { //记住当前控件坐标和宽高以及鼠标按下的坐标 QMouseEvent *mouseEvent = (QMouseEvent *)event; rectX = widget->x(); rectY = widget->y(); rectW = widget->width(); rectH = widget->height(); lastPos = mouseEvent->pos(); //判断按下的手柄的区域位置 if (rectLeft.contains(lastPos)) { pressedLeft = true; } else if (rectRight.contains(lastPos)) { pressedRight = true; } else if (rectTop.contains(lastPos)) { pressedTop = true; } else if (rectBottom.contains(lastPos)) { pressedBottom = true; } else if (rectLeftTop.contains(lastPos)) { pressedLeftTop = true; } else if (rectRightTop.contains(lastPos)) { pressedRightTop = true; } else if (rectLeftBottom.contains(lastPos)) { pressedLeftBottom = true; } else if (rectRightBottom.contains(lastPos)) { pressedRightBottom = true; } else { pressed = true; } } else if (event->type() == QEvent::MouseMove) { //改成用HoverMove识别 } else if (event->type() == QEvent::MouseButtonRelease) { //恢复所有 pressed = false; pressedLeft = false; pressedRight = false; pressedTop = false; pressedBottom = false; pressedLeftTop = false; pressedRightTop = false; pressedLeftBottom = false; pressedRightBottom = false; widget->setCursor(Qt::ArrowCursor); } } return QObject::eventFilter(watched, event);}三、效果图
到此,关于"Qt如何实现通用无边框拖动拉伸"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
区域
边框
鼠标
位置
标题
学习
坐标
控件
方位
可设
移动
自由
代码
形状
思路
更多
窗体
系统
帮助
复杂
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
博彦科技互联网金融
广州为想互联网科技有限公司
互联网科技公司华为
互联网和科技创新
互联网科技资迅
互联网科技的
高科技与互联网
奇酷互联网科技公司
北京互联网络科技有限公司
互联网科技论文
美国互联网黑科技
互联网科技ppt模板
顺玮阁互联网科技
佳云科技和互联网广告
消费医药和互联网科技
群盈汇互联网科技有限公司
互联网安防科技感
南京一网一红互联网科技有限公司
科技互联网行业投资研究
科技互联网专业工作会议发言稿
A股互联网金融科技公司
麻工互联网科技
银优互联网科技有限公司
互联网科技小额贷款
哈尔滨新华互联网科技
互联网科技股午盘股票涨跌
世界互联网科技公司市值排名
特约风豹互联网科技为什么扣费
互联网金融科技公司的优势
西安有哪些互联网高科技企业