Qt开源作品31-屏幕截图控件

摘要:
QWidget(父级){//this->menu->SLOT(saveScreen());menu->paintEvent(QPaintEvent*){intx=screen->inth=screen-&gt:green

一、前言

屏幕截图控件在我的很多项目中都有用到,尤其是嵌入式的系统上的软件,因为在嵌入式系统中,基本上系统都很精简,甚至连UI都没有,开机之后直接运行的就是Qt程序,很多时候需要对软件进行截图保存下来,用来编写文档和介绍,还有产品彩页之类的,毕竟在板子上直接运行的效果是最好的,还有一种办法是将系统编译成win的版本,用系统的截图来,但是嵌入式上很多代码其实很不方便在win上运行,甚至没法运行,而且还要外接很多接口来得到真正的运行效果,所以还是采用直接在板子上的Qt程序中直接集成截图的功能,需要的时候直接鼠标右键弹出来选择即可。

二、代码思路

ScreenWidget::ScreenWidget(QWidget *parent) : QWidget(parent)
{
    //this->setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint);

    menu = new QMenu(this);
    menu->addAction("保存当前截图", this, SLOT(saveScreen()));
    menu->addAction("保存全屏截图", this, SLOT(saveFullScreen()));
    menu->addAction("截图另存为", this, SLOT(saveScreenOther()));
    menu->addAction("全屏另存为", this, SLOT(saveFullOther()));
    menu->addAction("退出截图", this, SLOT(hide()));

    //取得屏幕大小
    screen = new Screen(QApplication::desktop()->size());
    //保存全屏图像
    fullScreen = new QPixmap();
}

void ScreenWidget::paintEvent(QPaintEvent *)
{
    int x = screen->getLeftUp().x();
    int y = screen->getLeftUp().y();
    int w = screen->getRightDown().x() - x;
    int h = screen->getRightDown().y() - y;

    QPainter painter(this);

    QPen pen;
    pen.setColor(Qt::green);
    pen.setWidth(2);
    pen.setStyle(Qt::DotLine);
    painter.setPen(pen);
    painter.drawPixmap(0, 0, *bgScreen);

    if (w != 0 && h != 0) {
        painter.drawPixmap(x, y, fullScreen->copy(x, y, w, h));
    }

    painter.drawRect(x, y, w, h);

    pen.setColor(Qt::yellow);
    painter.setPen(pen);
    painter.drawText(x + 2, y - 8, tr("截图范围:( %1 x %2 ) - ( %3 x %4 )  图片大小:( %5 x %6 )")
                     .arg(x).arg(y).arg(x + w).arg(y + h).arg(w).arg(h));
}

void ScreenWidget::showEvent(QShowEvent *)
{
    QPoint point(-1, -1);
    screen->setStart(point);
    screen->setEnd(point);

#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
    *fullScreen = fullScreen->grabWindow(QApplication::desktop()->winId(), 0, 0, screen->width(), screen->height());
#else
    QScreen *pscreen = QApplication::primaryScreen();
    *fullScreen = pscreen->grabWindow(QApplication::desktop()->winId(), 0, 0, screen->width(), screen->height());
#endif

    //设置透明度实现模糊背景
    QPixmap pix(screen->width(), screen->height());
    pix.fill((QColor(160, 160, 160, 200)));
    bgScreen = new QPixmap(*fullScreen);
    QPainter p(bgScreen);
    p.drawPixmap(0, 0, pix);
}

三、效果图

Qt开源作品31-屏幕截图控件第1张

四、开源主页

以上作品完整源码下载都在开源主页,会持续不断更新作品数量和质量,欢迎各位关注。

  1. 国内站点:https://gitee.com/feiyangqingyun/QWidgetDemo
  2. 国际站点:https://github.com/feiyangqingyun/QWidgetDemo
  3. 个人主页:https://blog.csdn.net/feiyangqingyun
  4. 知乎主页:https://www.zhihu.com/people/feiyangqingyun/

免责声明:文章转载自《Qt开源作品31-屏幕截图控件》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇性能测试三十二:监控之Java线程监控Vue的介绍&amp;amp;原理下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

通过chkrootkit学习如何在linux下检测RootKit

Rootkit是一种特殊的恶意软件,它的功能是在安装目标上隐藏自身及指定的文件、进程和网络链接等信息,比较多见到的是Rootkit一般都和木马、后门等其他恶意程序结合使用。Rootkit一词更多地是指被作为驱动程序,加载到操作系统内核中的恶意软件。 chkrootkit简介 chkrootkit是一个linux下检RootKit的脚本,在某些检测中会调用当...

WPF DataContext与Binding的关系

在前台UI创建一个Label绑定到myLabel <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsof...

通达OA 11.5 SQL注入漏洞复现

0x00 漏洞描述  通达OA 11.5存在sql注入 0x01 漏洞影响版本  通达oa 11.5 0x02 漏洞复现 1、下载通达OA 11.5 https://cdndown.tongda2000.com/oa/2019/TDOA11.5.exe,点击安装 2、创建一个普通账户test:test123456 3.1、id参数存在sql注入 利用条...

jdk1.6 和 jdk1.7 区别

1、JDK1.6 以前的版本只支持 byte、char、short、int、枚举,       JDK1.7 增加 String 类型 2、运用 List<String> tempList = new ArrayList<>(); 即泛型实例化类型自动推断    (1)在以前的版本中使用泛型类型,需要在声明并赋值的时候,两侧都加...

laravel队列常驻运行问题 queue:

方法:1 根据官方文档使用--daemon可让队列常驻运行。对应命令行执行php artisan queue:work --daemon; 但是当前命令还是会因为使用Ctrl+C。将进程给关闭。如何常驻在后台执行? 官方文档还让你安装 supervisor 你为什么不安装呢? 用 supervisor 可以常驻后台,并且监控 php artisan qu...

Layui数据表格动态加载操作按钮

效果:  方法一:绑定模版选择器 <div class="layui-card"> <div class="layui-card-body layui-row layui-col-space10"> <table lay-filter="deliveryTable"></table...