#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTimer> //定时期
#include <QPixmap> //图像类
#include <QDesktopWidget> //desktopwidget类提供对多头系统上屏幕信息的访问。
#include <QMessageBox>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_newSreenShotbutton_clicked();
void shotScreenSlot();
void on_savePicturebutton_clicked();
private:
Ui::MainWindow *ui;
QTimer *timer;
QPixmap pixmap;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QScreen> //The QScreen class is used to query screen properties qscreen类用于查询屏幕属性
#include <QFileDialog> //获取文件路径
#include <QDesktopServices> //桌面服务
#include <QClipboard> //剪切版
#include <QDebug>
//storagelocation
//把内容放到剪切版剪切版 进程通信
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_newSreenShotbutton_clicked()
{
if(ui->checkBox->isChecked()) // 单选框
{
this->hide();
this->timer = new QTimer;
//连接 定时期信号 和截屏槽函数
QObject::connect(timer,SIGNAL(timeout()),this,SLOT(shotScreenSlot()));
//启动定时器
this->timer->start(ui->spinBox->value()*1000);
}
else
{
qApp->beep();
}
/*
QClipboard *clipboard = QGuiApplication::clipboard();
QString originalText = clipboard->text();
...
clipboard->setText(newText);
*/
}
void MainWindow::shotScreenSlot()
{
//pixmap 当前应用程序所在屏幕 抓取
// QScreen::grabWindow();
// QScreen screen(this);
this->pixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
// 图片的大小 为label_3 的大小
ui->label_3->setPixmap(this->pixmap.scaled(ui->label_3->size()));
//打开系统剪切版
QClipboard *clipboard = QGuiApplication::clipboard();
//QString originalText = clipboard->text(); //获得剪切版内的文本
// ... 把图片放到剪切版
clipboard->setPixmap(this->pixmap);
qDebug()<<clipboard->pixmap().size();
this->show();
this->timer->stop();
}
void MainWindow::on_savePicturebutton_clicked()
{//QDesktopServices::storageLocation(QDesktopServices::DataLocation)
// QString filename=QFileDialog::getSaveFileName(this,"savefile",QDesktopServices::storageLocation(QDesktopServices::PicturesLocation);/home/centos/grapSreen/mainwindow.cpp:63: error: ‘storageLocation’ is not a member of ‘QDesktopServices’
QString filename=QFileDialog::getSaveFileName(this,"savefile","/home/centos/图片");
//pixmax 提供了保存接口
this->pixmap.save(filename);
}