QT实现截屏

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

指定截取区域

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

DrawScreen头文件:

#ifndef DRAWSCREEN_H
#define DRAWSCREEN_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

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 
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 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 
#include 
#include 
#include 

#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();

}

你可能感兴趣的:(QT,qt,开发语言)