剪切板实现进程间持续抓图转显

定时抓图进程,并转存到剪切板

snap.pro

#-------------------------------------------------
#
# Project created by QtCreator 2020-05-02T15:51:11
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = snap
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

SOURCES += 
        main.cpp 
        snap.cpp

HEADERS += 
        snap.h

FORMS += 
        snap.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

snap.h

#ifndef SNAP_H
#define SNAP_H

#include <QMainWindow>
#include <QTimer>
#include <QPixmap>

namespace Ui {
class snap;
}

class snap : public QMainWindow
{
    Q_OBJECT

public:
    explicit snap(QWidget *parent = nullptr);
    ~snap();
public slots:
    void onClickNewSnapSlot();
    void onTimerSnapSlot();
    void onClickSaveSnapSlot();
private:
    Ui::snap *ui;
    QTimer* m_timer;
    QPixmap m_pixMap;
};

#endif // SNAP_H

snap.cpp

#include "snap.h"
#include "ui_snap.h"
#include <QPixmap>
#include <QApplication>
#include <QDesktopWidget>
#include <QFileDialog>
#include <QScreen>
#include <QClipboard>

snap::snap(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::snap)
{
    ui->setupUi(this);

    connect(ui->newBtn, SIGNAL(clicked()), this, SLOT(onClickNewSnapSlot()));
    connect(ui->saveBtn, SIGNAL(clicked()), this, SLOT(onClickSaveSnapSlot()));
    connect(ui->exitBtn, SIGNAL(clicked()), this, SLOT(close()));
}

snap::~snap()
{
    delete ui;
}


void snap::onClickNewSnapSlot()
{
    m_timer = new QTimer();
    connect(this->m_timer, SIGNAL(timeout()), this, SLOT(onTimerSnapSlot()));
    m_timer->start(1000/24);
}

void snap::onClickSaveSnapSlot()
{
    QString fileName = QFileDialog::getSaveFileName(this, "save file", QDir::currentPath(), tr("Images (*.png *.xpm *.jpg)"));
    m_pixMap.save(fileName);
}

void snap::onTimerSnapSlot()
{
    //this->m_pixMap = QWidget::grab(QRect( QPoint( 0, 0 ), QSize( -1, -1 ) ));
    //this->m_pixMap = QPixmap::grabWindow(QApplication::desktop()->winId(), 0, 0, -1, -1);
    QScreen* screen = QGuiApplication::primaryScreen();
    this->m_pixMap = screen->grabWindow(QApplication::desktop()->winId(), 0, 0, -1, -1);
    ui->showLabel->setPixmap(this->m_pixMap.scaled(ui->showLabel->size()));

    QClipboard *clipboard = QApplication::clipboard();
    clipboard->setPixmap(this->m_pixMap);
}

main.cpp

#include "snap.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    snap w;
    w.show();

    return a.exec();
}

定时从剪切板取图进程

queryClipBoard.pro

#-------------------------------------------------
#
# Project created by QtCreator 2020-05-02T17:26:29
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = queryClipBoard
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

SOURCES += 
        main.cpp 
        queryclipboard.cpp

HEADERS += 
        queryclipboard.h

FORMS += 
        queryclipboard.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

queryclipboard.h

#ifndef QUERYCLIPBOARD_H
#define QUERYCLIPBOARD_H

#include <QMainWindow>
#include <QTimer>
#include <QPixmap>

namespace Ui {
class queryClipBoard;
}

class queryClipBoard : public QMainWindow
{
    Q_OBJECT

public:
    explicit queryClipBoard(QWidget *parent = nullptr);
    ~queryClipBoard();
private:
    virtual void contextMenuEvent(QContextMenuEvent* event);
private slots:
    void onQueryClickBoard();
    void onTimerQuery();
    void onClickSaveSnapSlot();
private:
    Ui::queryClipBoard *ui;
    QTimer* m_timer;
    QPixmap m_pixmap;
};

#endif // QUERYCLIPBOARD_H

queryclipboard.cpp

#include "queryclipboard.h"
#include "ui_queryclipboard.h"
#include <QClipboard>
#include <QPixmap>
#include <QMenu>
#include <QAction>
#include <QFileDialog>
#include <QCursor>

queryClipBoard::queryClipBoard(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::queryClipBoard)
{
    m_timer = nullptr;
    ui->setupUi(this);
    connect(ui->startBtn, SIGNAL(clicked()), this, SLOT(onQueryClickBoard()));
}

queryClipBoard::~queryClipBoard()
{
    delete ui;
}

void queryClipBoard::onQueryClickBoard()
{
    if(m_timer)
    {
        m_timer->stop();
        disconnect(m_timer, SIGNAL(timeout()), this, SLOT(onTimerQuery()));
        delete m_timer;
    }
    m_timer = new QTimer(this);
    connect(m_timer, SIGNAL(timeout()), this, SLOT(onTimerQuery()));
    m_timer->start(200);
}

void queryClipBoard::onTimerQuery()
{
    QClipboard *clipboard = QApplication::clipboard();
    m_pixmap = clipboard->pixmap();
    ui->showLabel->setPixmap(m_pixmap);
}

void queryClipBoard::onClickSaveSnapSlot()
{
    QString fileName = QFileDialog::getSaveFileName(this, "save file", QDir::currentPath(), tr("Images (*.png *.xpm *.jpg)"));
    m_pixmap.save(fileName);
}

void queryClipBoard::contextMenuEvent(QContextMenuEvent* event)
{
    QMenu* menu = new QMenu();
    QAction* action = new QAction();
    action->setText(QString("保存"));
    connect(action, SIGNAL(triggered()), this, SLOT(onClickSaveSnapSlot()));

    menu->addAction(action);
    menu->exec(QCursor::pos());
    delete menu;
    delete action;
}

main.cpp

#include "queryclipboard.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    queryClipBoard w;
    w.show();

    return a.exec();
}
原文地址:https://www.cnblogs.com/kuikuitage/p/12820365.html