一、效果图
二、窗口特点与用途
- 窗口背景透明,窗口中的控件并不透明。
- 可以使用鼠标拖动窗口位置;
- 可以通过拖放角任意改变窗口大小;
- 可以正常放置任何控件或留空;
- 此透明窗口用于实现截图功能或截取视频的功能;
- 点击例子中透明窗口里的按钮即可保存透明选取的截图到执行文件目录;
- 支持windows平台和MacOS平台
三、制作原理与方法
- 使用以下代码实现窗口透明且窗口内控件不透明
setWindowFlags(Qt::Dialog |
Qt::FramelessWindowHint |
Qt::NoDropShadowWindowHint);
setAttribute(Qt::WA_NoSystemBackground, true);
setAttribute(Qt::WA_TranslucentBackground, true);
- 使用paintEvent绘制边框和移动标志
- 使用mousePressEvent,mouseMoveEvent, mouseReleaseEvent来检测鼠标移动和点击。从而实现移动窗口,缩放窗口。
- 使用如下代码实现截图功能
QScreen *screen = QGuiApplication::primaryScreen();
QPixmap p = screen->grabWindow(QApplication::desktop()->winId(),
capture_rect.x(),
capture_rect.y(),
capture_rect.width(),
capture_rect.height());
- 区域设计图
四、本项目源码
五、参考文章
- 设置了透明以后,会有严重残影
https://www.cnblogs.com/findumars/p/6411273.html - Qt不带标题栏(FramelessWindowHint)的窗体移动及调整大小
https://blog.csdn.net/luols/article/details/48733721 - Qt实现半透明、无边框、可自由移动、不规则的窗体
https://blog.csdn.net/qq_37385181/article/details/82894077