Qt探索之旅(八)屏幕截图与剪切板进程间通信

URL 统一资源定位符
(Uniform Resource Locator,缩写为URL),又叫做网页地址,是互联网上标准的资源的地址(Address)。互联网上的每个文件都有一个唯一的URL,它包含的信息指出文件的位置以及浏览器应该怎么处理它。

SHOTSCRREN屏幕截图功能,可实现帧动画

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTimer>
#include <QPixmap>
#include <QDesktopWidget>
#include <QMessageBox>//消息对话框
#include <QFileDialog>//文件对话框
#include <QDesktopServices>
#include <QClipboard>
#include <QDebug>
#include <QContextMenuEvent>
#include <QMenu>
#include <QAction>
#include <QCursor>
#include <QProcess>

//storageLocation
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

protected:
    void contextMenuEvent(QContextMenuEvent *event);


private slots:
    void on_newScreenShotButton_clicked();
    void shotScreenSlot();
    void savePictureSlot();//保存截图
    void startNotepadSlot();

private:
    Ui::MainWindow *ui;
    QTimer *timer;
    QPixmap pixmap;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QObject::connect(ui->savePictureButton, SIGNAL(clicked()), this, SLOT(savePictureSlot()));
}

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

void MainWindow::on_newScreenShotButton_clicked()
{
    if (ui->checkBox->isChecked())
    {
        this->hide();//this->show();
        this->timer = new QTimer;
        QObject::connect(this->timer, SIGNAL(timeout()), SLOT(shotScreenSlot()));
        this->timer->start(ui->spinBox->value() * 1000);//控制延时时间,由spinBox的值控制
        //this->timer->start(1000);
    }
    else
    {
        qApp->beep();//泵的一声
    }
}


void MainWindow::shotScreenSlot()
{
    this->pixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
    ui->screenLable->setPixmap(this->pixmap.scaled(ui->screenLable->size()));

    QClipboard *clipboard = QApplication::clipboard();//截图到剪切板
   // QString originalText = clipboard->text();

   // qDebug() << "current clipboard text is " << originalText;
    clipboard->setPixmap(this->pixmap);//系统剪切板中就有图片的信息了,可以直接粘贴发送给别人

    this->show();
    this->timer->stop();
}


void MainWindow::savePictureSlot()
{
    QString fileName = QFileDialog::getSaveFileName(this, "Save File", QDesktopServices::storageLocation(QDesktopServices::PicturesLocation));
    this->pixmap.save(fileName);
}


void  MainWindow::contextMenuEvent(QContextMenuEvent *event)
{
    QMenu *menu = new QMenu(this);
    QAction *action = new QAction(this);//鼠标右键可以显示功能菜单(第一个功能)
    QAction *processAction = new QAction(this);//鼠标右键可以显示功能菜单(第二个功能)
    QObject::connect(processAction, SIGNAL(triggered()), this, SLOT(startNotepadSlot()));
    QObject::connect(action, SIGNAL(triggered()), this, SLOT(savePictureSlot()));
    action->setText("Save As");
    processAction->setText("start notepad");
    menu->addAction(action);//添加动作
    menu->addSeparator();
    menu->addAction(processAction);
    menu->exec(QCursor::pos());
}


void  MainWindow::startNotepadSlot()
{
    QProcess *process = new QProcess;
    process->start("notepad.exe");
}

main.cpp

#include <QtGui/QApplication>
#include "mainwindow.h"

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

    return a.exec();
}

PLAYER屏幕监控播放功能
mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTimer>//定时器
#include <QClipboard>//剪切板
#include <QPixmap>//图片


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;
    QPixmap pixmap;
    QTimer *timer;
private slots:
    void fetchPictureSlot();
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->timer = new QTimer;
    QObject::connect(this->timer, SIGNAL(timeout()), this, SLOT(fetchPictureSlot()));
    this->timer->start(200);
}

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


void MainWindow::fetchPictureSlot()
{
    QClipboard *board = QApplication::clipboard();
    this->pixmap = board->pixmap();//获取图片
    ui->playerLabel->setPixmap(this->pixmap.scaled(ui->playerLabel->size()));//设置图片大小
}

main.cpp

#include <QtGui/QApplication>
#include "mainwindow.h"

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

    return a.exec();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

浮生卍流年

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

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

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

打赏作者

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

抵扣说明:

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

余额充值