当前位置:网站首页>QT GrabWindow截取屏幕
QT GrabWindow截取屏幕
2022-08-11 05:15:00 【双子座断点】
QT 使用GrabWindow截取屏幕,本文提供代码示例和Demo程序。
编译环境:QT6/5 运行环境:X64/32 WIN10/11
本文作者原创,转载请附上文章出处与本文链接。
QT GrabWindow截取屏幕目录
1 .CPP
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//隐藏标题栏
setWindowFlags(Qt::FramelessWindowHint);//无边框 置顶
//设置窗口背景透明
setAttribute(Qt::WA_TranslucentBackground);
//全屏显示
showFullScreen();
//设置样式
this->setStyleSheet("#Widget{background-color: rgba(0, 0, 0, 150);}");
}
Widget::~Widget()
{
delete ui;
}
void Widget::paintEvent(QPaintEvent *p1)
{
//绘制样式
QStyleOption opt;
opt.initFrom(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);//绘制样式
if(isPressedWidget)
{
//定义画笔
QPen pen;
pen.setWidth(5);
pen.setColor(QColor("#00B0AE"));
pen.setStyle(Qt::DashDotLine);
p.setPen(pen);
//创建画刷
QBrush brush;
brush.setColor(QColor("#00B0AE"));
brush.setStyle(Qt::Dense6Pattern);
p.setBrush(brush);
QRect tempRt(m_startPT, m_endPT);
p.drawRect(tempRt);
}
}
void Widget::mousePressEvent(QMouseEvent *event)
{
m_endPT = m_startPT = event->pos();
isPressedWidget = true; // 当前鼠标按下的即是QWidget而非界面上布局的其它控件
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
QPoint tmp_pos=event->pos();
if(tmp_pos.x()>m_startPT.x() || tmp_pos.y()>m_startPT.y())
{
m_endPT = event->pos();
}
this->update();
}
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
isPressedWidget = false; // 鼠标松开时,置为false
QRect rect(m_startPT, m_endPT);
qDebug()<<"选择的范围:"<<rect;
qDebug()<<"rect.x(): "<<rect.x();
qDebug()<<"rect.x(): "<<rect.y();
qDebug()<<"rect.x(): "<<rect.height();
qDebug()<<"rect.x(): "<<rect.width();
QScreen *screen = QApplication::primaryScreen();
QPixmap pximap = screen->grabWindow(0, rect.x() ,rect.y(),rect.width() , rect.height());
QImage image = pximap.toImage();
image.save("D:\\CC.jpg");
close();
}
/*
工程: HTTP_Request
日期: 2022-8-9
作者: 双子座断点
环境: win10 QT5.12.6
功能: 进入全屏
*/
void Widget::on_pushButton_clicked()
{
showFullScreen();
}
/*
工程: HTTP_Request
日期: 2022-8-9
作者: 双子座断点
环境: win10 QT5.12.6
功能: 进入全屏
*/
//void Widget::on_pushButton_2_clicked()
//{
// showNormal();
//}
/*
工程: HTTP_Request
日期: 2022-8-9
作者: 双子座断点
环境: win10 QT5.12.6
功能: 进入全屏
*/
void Widget::on_pushButton_close_clicked()
{
close();
}
2 .h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QStyleOption>
#include <QPainter>
#include <QMouseEvent>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
protected:
//截取鼠标事件绘制窗口位置. 因为标题栏隐藏后.窗口是无法拖动的。
void mouseReleaseEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
void paintEvent(QPaintEvent *p);
private slots:
void on_pushButton_clicked();
//void on_pushButton_2_clicked();
void on_pushButton_close_clicked();
private:
Ui::Widget *ui;
bool isPressedWidget;
QPoint m_startPT;
QPoint m_endPT;
};
#endif // WIDGET_H
3 软件运行截图

项目源代码下载链接:CSDN
边栏推荐
- 【win10+cuda7.5+cudnn6.0安装caffe⑥】报错及处理方式
- 总结:交叉验证
- vftpd本地可以连接,远程连接超时的解决
- [Embedded open source library] The use of MultiButton, an easy-to-use event-driven button driver module
- 面试题整理
- Redis-使用jedis连接linux中redis服务器失败的解决方案
- Map根据key值排序
- Flask framework learning: trailing slashes for routes
- Tips to make your code more and more taller and taller - code specification, you have to know
- 实战noVNC全过程操作(包含遇到的问题和解决)
猜你喜欢

【嵌入式开源库】MultiTimer 的使用,一款可无限扩展的软件定时器

Oracle中如何用一个表的数据更新另一个表中的数据_转载

Django--20 implements Redis support, context, and interaction of context and interface

Flask framework learning: trailing slashes for routes

【win10+cuda7.5+cudnn6.0安装caffe②】安装Visual Studio 2013和caffe

第二篇 DS5 Armv8 样例工程报错之GCC编译

Delphi7 learning record - demo example

Flask框架学习:模板继承

(二)性能实时监控平台搭建(Grafana+Prometheus+Jmeter)

(1) Docker installs Redis in practice (one master, two slaves, three sentinels)
随机推荐
【动态代理】CGLIB 动态代理的使用及原理
【ARM】rk3399挂载nfs报错
【Mysql】----基础练习
Idea 2021.3.3版本文件目录展开
Delphi7 learning record - demo example
redis分布式锁
【分享】一个免费语料库
面试宝典二:nlp常见知识点
原生态mongo连接查询代码
Solidrun hummingboard制作SD卡
面试题整理
2022年Android面试中最常问的问题是什么?
Core Data 多线程设计
Mysql introductory exercise
将double类型的数据转为字符串
普林斯顿微积分读本05第四章--求解多项式的极限问题
Redis-使用jedis连接linux中redis服务器失败的解决方案
Linux中安装redis
课堂练习--0708
oracle tablespace and user creation