QT编写简易截图工具

QT编写简易截图工具

Windows和Linux下编译通过,版本5.9.1


##设计界面
新建Widget项目,进入ui设计界面,添加QLabel和两个QPushButton,布局如下:
QT编写简易截图工具_第1张图片

Button重命名为pushButton_save和pushButton_snip。
加入menu如下,添加action并设置快捷键。
QT编写简易截图工具_第2张图片

##创建截图窗口类

Markdown及扩展

添加新文件,选择QT Designer Form Class,如下,继承QWidget,类名修改为ScreenShot,作为截图窗口类。
QT编写简易截图工具_第3张图片

##添加代码
转到mainwindow.h在类中添加重绘事件函数:

mainwindow.h

protected:
    void paintEvent(QPaintEvent *);

另外添加按钮和菜单的槽函数:

mainwindow.h

private slots:
    void on_actionSave_triggered();

    void on_actionCopy_triggered();

    void on_pushButton_snip_clicked();

    void on_pushButton_save_clicked();

    void on_actionSnip_triggered();

    void timerSlot();                   //定时器处理槽函数

    void screenShotHideSlot();          //截图窗口消失槽函数

添加成员变量:(注意要加入头文件#include screenshot.h 和 QTimer)

mainwindow.h

private:
    Ui::MainWindow *ui;
    
    ScreenShot *wScreen;     //截图窗口类
    
    QTimer timer;           //定时器
    
    QPixmap tmpPix;         //用于保存截图的临时变量

转到mainwindow.cpp实现成员函数
首先添加所需要用到的头文件:

mainwindow.cpp

#include "screenshot.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 

再在构造函数中添加如下代码:

mainwindow.cpp

    this->setWindowTitle(tr("截图工具"));

    wScreen = new ScreenShot;

    wScreen->hide();     //初始化不显示

    QDesktopWidget *deskWidget = QApplication::desktop();

    QRect deskRect = deskWidget->geometry();            //获取桌面大小

    resize(deskRect.width() / 3, deskRect.height() / 3);//设置窗口大小

    connect(&timer,SIGNAL(timeout()),this,SLOT(timerSlot()));

然后分别实现其他成员函数如下:

mainwindow.cpp

//Snip按钮槽函数
void MainWindow::on_pushButton_snip_clicked()
{
    this->hide();   //截图开始 隐藏主窗口
    timer.start(20);//这里加定时器是因为窗口不会立即隐藏
}
//重绘事件函数
void MainWindow::paintEvent(QPaintEvent *)
{
    if(tmpPix.isNull() == false)    //若截图不为空
    {
        //将截图大小缩放到label大小 可看scaled的帮助文档
        QPixmap scalePix = tmpPix.scaled(ui->label->size(),
                                         Qt::KeepAspectRatio,Qt::SmoothTransformation);
        //label显示截图
        ui->label->setPixmap(scalePix);
    }
}
//save按钮槽函数
void MainWindow::on_pushButton_save_clicked()
{
    if(tmpPix.isNull() == false)    //若截图不为空
    {
        //打开保存文件对话框
        QString fileName = QFileDialog::getSaveFileName(this,
                                "save","../",tr("Images (*.png *.xpm *.jpg)"));
        //保存截图
        tmpPix.save(fileName);
    }
    else
    {
        QMessageBox::information(this,"warning","no screenshot");
    }
}
//定时器处理函数
void MainWindow::timerSlot()
{
    //首先关闭定时器
    timer.stop();

    //获取屏幕的Pixmap
    QPixmap screen = QPixmap::grabWindow(QApplication::desktop()->winId());

    //绑定截图窗口关闭信号与主窗口处理槽函数
    connect(wScreen,SIGNAL(closeSignal()),this,SLOT(screenShotHideSlot()));

    //截图窗口截取屏幕
    wScreen->shotScreen(screen);

    //显示截图窗口
    wScreen->show();
}
//截图窗口消失处理槽函数
void MainWindow::screenShotHideSlot()
{
    //显示主窗口
    this->show();

    //获取截图
    tmpPix = wScreen->getShotPixmap();

    //label显示截图
    QPixmap scalePix = tmpPix.scaled(ui->label->size(),
                                     Qt::KeepAspectRatio,Qt::SmoothTransformation);

    ui->label->setPixmap(scalePix);
}
//菜单action函数
void MainWindow::on_actionSnip_triggered()
{
    on_pushButton_snip_clicked();
}

void MainWindow::on_actionSave_triggered()
{
    on_pushButton_save_clicked();
}

void MainWindow::on_actionCopy_triggered()
{
	if(tmpPix.isNull() == true)
    {
        QMessageBox::information(this,"warning","no image");
        return;
    }
    QClipboard *clipboard = QApplication::clipboard();

    clipboard->setImage(tmpPix.toImage());
}

有部分函数需要在截图窗口类ScreenShot类中实现,现在实现该类,转到screenshot.h中,添加所需头文件:

screenshot.h

#include 
#include 

再添加如下成员函数和变量:

screenshot.h

public:
    void shotScreen(QPixmap &pix);
    QPixmap snipScreen(QPixmap &pix,QPoint &start,QPoint &end);
    QPixmap& getShotPixmap();

signals:
    void closeSignal();

protected:
    void paintEvent(QPaintEvent *);
    void mousePressEvent(QMouseEvent *e);
    void mouseMoveEvent(QMouseEvent *e);
    void mouseReleaseEvent(QMouseEvent *);
    
private:
    QPoint startPoint;
    QPoint endPoint;
    
    QPixmap screen;
    QPixmap shotPix;
    
    QTimer timer;

转到screenshot.cpp实现成员函数:
添加所需头文件:

screenshot.cpp

#include 
#include 
#include 
#include 

在构造函数中加入:

    //设置窗口没有标题栏
    this->setWindowFlags(Qt::FramelessWindowHint);
    //定时器信号和槽
    connect(&timer,SIGNAL(timeout()),this,SLOT(update()));

其他函数:

screenshot.cpp

//设置截图窗口
void ScreenShot::shotScreen(QPixmap &screenPix)
{
    this->screen = screenPix;

    //设置窗口大小 和屏幕大小一致
    this->resize(screenPix.size());

    //设置遮罩
    this->setMask(QRegion(0,0,width(),height()));
}

//绘图事件函数
void ScreenShot::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.drawPixmap(0,0,screen);

    //设置画笔样式
    QPen pen;
    pen.setStyle(Qt::DashLine);
    pen.setColor(Qt::red);

    painter.setPen(pen);

    //所需截图区域用矩形框显示
    painter.drawRect(QRect(startPoint,endPoint));
}

//鼠标事件函数
void ScreenShot::mousePressEvent(QMouseEvent *e)
{
    if(e->button() == Qt::LeftButton)
    {
        //鼠标左键按下 设置起始点
        timer.start(20);
        startPoint = e->pos();
        endPoint = startPoint;
    }
    else if(e->button() == Qt::RightButton)
    {
        //鼠标右键按下 隐藏截图窗口 并向主窗口发送关闭消息
        hide();
        emit closeSignal();
    }
}

void ScreenShot::mouseMoveEvent(QMouseEvent *e)
{

    if(e->buttons() & Qt::LeftButton)
    {
        endPoint = e->pos();
    }
}

void ScreenShot::mouseReleaseEvent(QMouseEvent *e)
{
    endPoint = e->pos();
    if(QMessageBox::Yes == QMessageBox::question(this,"question","yes to shot?"))
    {
        shotPix = snipScreen(screen,startPoint,endPoint);
        this->hide();
        startPoint = QPoint(0,0);
        endPoint = startPoint;
        emit closeSignal();
    }
}

//截图函数
QPixmap ScreenShot::snipScreen(QPixmap &pix, QPoint &start, QPoint &end)
{
    int w = abs(start.x() - end.x());
    int h = abs(start.y() - end.y());

    QPixmap tmp = QPixmap(w,h);

    QPainter painter(&tmp);

    //将截图部分绘制到tmp中
    painter.drawPixmap(QPoint(0,0),pix,QRect(start,end));

    return tmp;
}

//获取截图函数
QPixmap& ScreenShot::getShotPixmap()
{
    return shotPix;
}

##编译运行
最后编译运行结果如下:
QT编写简易截图工具_第4张图片

点击按钮就可以单击鼠标左键拖动截图。
附源码下载: http://download.csdn.net/download/weddyhuo/10124652

你可能感兴趣的:(qt学习)