qt截图grapWindow,操作系统剪切版QClipBoard实现进程间通信

QPixmap::grapWindow(winID) 存放一个图片
QDesktopWidget 获得当前程序所在窗口id pid
每个窗口有winID()
// 3
pixmap scaled 比例缩放
QClipboard 操作系统剪切版
            m_pic = QPixmap::grabWindow(w.winId());
            QClipboard *clipboard = QGuiApplication::clipboard();
//            QString originalText = clipboard->text();
//            qDebug() << originalText;
            clipboard->setPixmap(m_pic);
 

.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDesktopWidget>
#include <QFileDialog>
#include <QScreen>
#include <QDir>
#include <QClipboard>
#include <QDebug>

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

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

void MainWindow::on_newPicButton_clicked()
{
    if (ui->radioButtonHide->isChecked()) {
        timer = new QTimer();
        //timer->setInterval(ui->spinBox->value()*1000);

        //this->hide();
        timer->start(1000/3);
        //int id = startTimer(ui->spinBox->value()*1000);
        connect(this->timer,&QTimer::timeout,this,[=]{
            //QDesktopWidget w;
            m_pic = QPixmap::grabWindow(QApplication::desktop()->winId());

            // 保存到剪切版
            //QClipboard *clipboard = QGuiApplication::clipboard();

//            QString originalText = clipboard->text();
//            qDebug() << originalText;
            //clipboard->setPixmap(m_pic);


            QSize size = ui->labelShowPic->size();
            ui->labelShowPic->setPixmap(m_pic.scaled(size));
            //timer->stop();
            //this->show();
        });


    }
    else {
        qApp->beep();// 嘀嘀响
    }
}

void MainWindow::on_savePicButton_clicked()
{
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
                               QDir::currentPath(),
                               tr("Images (*.png *.xpm *.jpg)"));

    if (!fileName.isEmpty()) {
        m_pic.save(fileName);
    }

}

void MainWindow::on_exitButton_clicked()
{
    this->close();
}

 

.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

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

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    void on_newPicButton_clicked();

    void on_savePicButton_clicked();

    void on_exitButton_clicked();

private:
    Ui::MainWindow *ui;

    QTimer *timer;
    QPixmap m_pic;
};

#endif // MAINWINDOW_H

 

剪切版QClipBoard实现进程间通信

 

#include "widget.h"
#include "ui_widget.h"


Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    m_timer = new QTimer(this);
    m_timer->start(200);

    connect(m_timer,&QTimer::timeout,this,&Widget::fetchPicFromClipBoard);
}

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

void Widget::fetchPicFromClipBoard()
{
    QClipboard *board = QGuiApplication::clipboard();
    QPixmap pix = board->pixmap();
    ui->label->setPixmap(pix.scaled(ui->label->size()));
}

 

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

#include <QClipboard>
#include <QTimer>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();

    void fetchPicFromClipBoard();

private:
    Ui::Widget *ui;

    QTimer *m_timer;
};

#endif // WIDGET_H

 

posted @ 2019-06-23 22:29  狂奔~  阅读(558)  评论(0编辑  收藏  举报