QT实现截屏

分两步,首先从屏幕中获取指定的区域,第二,根据指定区域抓取图片。

指定截取区域

思路:创建一个窗口类DrawScreen,让用户自己可以画出指定的区域。

DrawScreen头文件:

#ifndef DRAWSCREEN_H
#define DRAWSCREEN_H

#include <QDebug>
#include <QScreen>
#include <QPainter>
#include <QShowEvent>
#include <QPaintEvent>
#include <QApplication>
#include <QMouseEvent>
#include <QMainWindow>
#include <QGuiApplication>

class DrawScreen : public QMainWindow
{
    Q_OBJECT
public:
    explicit DrawScreen(QWidget *parent = nullptr);

public:
    QPixmap pixmap;

    //鼠标按下时的位置
    QPoint pressedPos;

    //鼠标释放时的位置
    QPoint releasedPos;

    //鼠标当前的位置
    QPoint currentPos;
signals:
    void gotRectScreen(QRect);

protected:
    void paintEvent(QPaintEvent*);
    void mousePressEvent(QMouseEvent*);
    void mouseReleaseEvent(QMouseEvent*);
    void mouseMoveEvent(QMouseEvent*);
    void showEvent(QShowEvent*);
};

#endif // DRAWSCREEN_H

当鼠标释放时发送信号gotRectScreen(QRect),此信号的参数就是选取的屏幕区域。

DrawShow实现:

#include "drawscreen.h"
#include <QDesktopWidget>
DrawScreen::DrawScreen(QWidget *parent)
    : QMainWindow{parent}
{
    this->showFullScreen();
}

void DrawScreen::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    painter.setPen(QPen(Qt::red,5));

    if (!pixmap.isNull())
        painter.drawPixmap(this->rect(),pixmap);

    if (pressedPos.x() >=0 && pressedPos.y() >=0)
        painter.drawRect(QRect(pressedPos,currentPos));
}

void DrawScreen::showEvent(QShowEvent *)
{
    pressedPos = currentPos = QPoint(-1,-1);

    QScreen *screen = QGuiApplication::primaryScreen();
    //QList<QScreen*> screens =  QGuiApplication::screens();
    //QScreen *screen = screens.at(0);
    this->pixmap = screen->grabWindow(0,0,0,-1,-1);
}

void DrawScreen::mousePressEvent(QMouseEvent* event)
{
    pressedPos = releasedPos = currentPos = event->pos();

    repaint();
}

void DrawScreen::mouseReleaseEvent(QMouseEvent* event)
{
    releasedPos = event->pos();

    int _x,_y,_w,_h;
    if (releasedPos.x() > pressedPos.x()){
        _x = pressedPos.x();
        _w = releasedPos.x() - pressedPos.x();
    }
    else{
        _x = releasedPos.x();
        _w = pressedPos.x() - releasedPos.x();
    }

    if (releasedPos.y() > pressedPos.y()){
        _y = pressedPos.y();
        _h = releasedPos.y() - pressedPos.y();
    }
    else{
        _y = releasedPos.y();
        _h = pressedPos.y() - releasedPos.y();
    }

    if (_w > 0 && _h > 0)
        emit(gotRectScreen(QRect(_x,_y,_w,_h)));

    repaint();
}

void DrawScreen::mouseMoveEvent(QMouseEvent* event)
{
    currentPos = event->pos();

    repaint();
}

一旦DrawShow窗口显示出来,则获取整个屏幕图片,并全屏显示图片,所以DrawShow创建时需要先隐藏。

抓取指定区域图片

创建一个窗口项目,链接gotRectScreen信号和槽函数,在槽函数中实现选取区域图片的抓取。

头文件:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QScreen>
#include <QPaintEvent>
#include <QGuiApplication>
#include <QMainWindow>

#include "drawscreen.h"

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    DrawScreen *drawScreen;//用来截取屏幕,创建后需要立刻隐藏
public slots:
    void onGotRectScreen(QRect);//响应DrawScreen类的gotRectScreen信号(已经获取到区域)
};
#endif // MAINWINDOW_H

实现:

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    //用于确定抓取屏幕范围的类
    drawScreen = new DrawScreen;
    connect(drawScreen,&DrawScreen::gotRectScreen,this,&MainWindow::onGotRectScreen);
    drawScreen->hide();
}

void MainWindow::onGotRectScreen(QRect rect)
{
    //隐藏drawScreen
    drawScreen->hide();

    //根据截取的区域抓取屏幕
    QScreen *screen = QGuiApplication::primaryScreen();
    QImage image = screen->grabWindow(0,rect.left(),rect.top(),rect.width(),rect.height()).toImage();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

视图猿人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值