QT-------截图工具

screenshot.h source code:

#ifndef SCREENSHOT_H
#define SCREENSHOT_H
 
#include <QWidget>
#include <QMainWindow>
#include <QTimer>
#include <QPixmap>
#include <QMessageBox>
#include <QDesktopWidget>
#include <QFileDialog>
#include <QDesktopServices>
#include <QDir>
#include <QDebug>
#include <QClipboard>
#include <QContextMenuEvent>
#include <QMenu>
#include <QAction>
#include <QCursor>
#include <QProcess>
 
namespace Ui {
class screenshot;
}
 
class screenshot : public QWidget
{
    Q_OBJECT
 
public:
    explicit screenshot(QWidget *parent = 0);
    ~screenshot();
protected:
    void contextMenuEvent(QContextMenuEvent *event);
private slots:
    void on_pushButton_clicked();
    void shotScreenSlot();
    void savePictureSlot();
    void startNotepadSlot();
    void on_pushButton_3_clicked();
 
private:
    Ui::screenshot *ui;
    QTimer *timer;
    QPixmap pixmap;//截取图片
};
 

#endif // SCREENSHOT_H

screenshot.cpp source code:

#include "screenshot.h"
#include "ui_screenshot.h"
 
screenshot::screenshot(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::screenshot)
{
    ui->setupUi(this);
    QObject::connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(savePictureSlot()));
}
 
screenshot::~screenshot()
{
    delete ui;
}
 
void screenshot::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(processAction);
    menu->addAction(action);
    menu->exec(QCursor::pos());
 
}
void screenshot::on_pushButton_clicked()
{
    if(ui->checkBox->isChecked())
    {
        this->hide();//隐葳窗体
        this->timer=new QTimer;
        QObject::connect(this->timer,SIGNAL(timeout()),SLOT(shotScreenSlot()));
        this->timer->start(1000/5);
    }
    else
    {
        qApp->beep();//泵的一声
    }
}
 
void screenshot::shotScreenSlot()
{
    //pixmap
    this->pixmap=QPixmap::grabWindow(QApplication::desktop()->winId());
    ui->label->setPixmap(this->pixmap.scaled(ui->label->size()));
    QClipboard *clipboard=QApplication::clipboard();
    clipboard->setPixmap(this->pixmap);
    this->show();
    this->timer->stop();
}
 
void screenshot::savePictureSlot()
{
    //QString fileName=QDir::currentPath();
    QString fileName=QFileDialog::getSaveFileName(this,tr("Save File"),"./未命命.bmp",tr("images(*.bmp)"));
    this->pixmap.save(fileName);
}
 
void screenshot::startNotepadSlot()
{
    QProcess *process=new QProcess;
    process->start("notepad.exe");
}
void screenshot::on_pushButton_3_clicked()
{
    this->close();
}
 

main.cpp source code:

#include "screenshot.h"
#include <QApplication>
#include <QWidget>
#include <QDir>
#include <QIcon>
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QString strPath=QApplication::applicationDirPath();
    strPath+="/convert.png";
    a.setWindowIcon(QIcon(strPath));
    screenshot w;
    w.setMaximumSize(884,765);
    w.setMinimumSize(884,765);
    w.show();
    return a.exec();
}
 
 


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值