qt截图功能

1.头文件

#pragma once

#include <QtWidgets/QWidget>
#include "ui_WidgetTest4.h"
#include <QPaintEvent>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QLabel>
#include <QToolBar>


class WidgetTest4 : public QWidget
{
    Q_OBJECT

public:
    WidgetTest4(QWidget *parent = Q_NULLPTR);
    void paintEvent(QPaintEvent*);          //绘图事件
    void mouseMoveEvent(QMouseEvent*);      //鼠标移动事件
    void mousePressEvent(QMouseEvent*);     //鼠标按下事件
    void keyPressEvent(QKeyEvent*);         //键盘按下事件
    void mouseReleaseEvent(QMouseEvent*);   //鼠标弹起事件
    void init();                            //初始化
    void createToolBar();                   //创建工具栏
    void connectSlots();                    //连接槽函数
private:
    Ui::WidgetTest4Class ui;
    QToolBar *m_pToolBar;   //工具栏
    QAction *m_pActSave;    //保存
    QAction *m_pActQuit;    //退出
    QAction* m_pFinish;     //完成

    QPoint m_ptFrist;   //第一次按下坐标
    QPoint m_ptEnd;     //结束点坐标
    bool m_bPress;      //是否按下
    QLabel* m_pLabel;   //坐标值
private slots:
    void saveFile();    //保存文件
    void quit();        //退出
    void finish();      //完成
};

2.源文件

#include "WidgetTest4.h"
#include <QPainter>
#include <QDesktopWidget>
#include <math.h>
#include <QScreen>
#include <QFileDialog>
#include <QWindow>
#include <QClipboard>
WidgetTest4::WidgetTest4(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);
    
    init();
    createToolBar();
    connectSlots();
    //设置主程序界面图片(截取屏幕图片)
    QString strPath = QCoreApplication::applicationDirPath();
    QScreen* screen = QGuiApplication::primaryScreen();
    screen->grabWindow(0).save(strPath + "/temp.bmp");
    QPalette palette = this->palette();
    QImage bkImage(strPath + "/temp.bmp");
    QImage fillBkImage = bkImage.scaled(this->width(), this->height(), Qt::IgnoreAspectRatio);
    palette.setBrush(QPalette::Window, QBrush(fillBkImage));
    this->setPalette(palette);

}

void WidgetTest4::connectSlots()
{
    connect(m_pActSave, SIGNAL(triggered()), this, SLOT(saveFile()));
    connect(m_pActQuit, SIGNAL(triggered()), this, SLOT(quit()));
    connect(m_pFinish, SIGNAL(triggered()), this, SLOT(finish()));
}

void WidgetTest4::init()
{
    m_bPress = false;   //没有按下
    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint); //设置窗体无标题栏
    QDesktopWidget* pDesktopWidget = QApplication::desktop();               //获取当前桌面
    //QRect deskRect = QApplication::desktop()->availableGeometry();        //获取可用桌面大小(不包含任务栏)
    QRect screenRect = QApplication::desktop()->screenGeometry();           //获取主屏幕分辨率(包含任务栏)
    resize(screenRect.width(), screenRect.height());                        //设置窗体大小

    //label的背景色和文字颜色
    m_pLabel = new QLabel(this);
    m_pLabel->setStyleSheet("QLabel {background-color: black;color:white;}");
}

void WidgetTest4::createToolBar()
{
    //工具条
    m_pToolBar = new QToolBar(this);
    m_pActSave = new QAction(QStringLiteral("保存"));
    m_pActQuit = new QAction(QStringLiteral("退出"));
    m_pFinish = new QAction(QStringLiteral("完成"));
    m_pToolBar->addAction(m_pActSave);
    m_pToolBar->addAction(m_pActQuit);
    m_pToolBar->addAction(m_pFinish);
    m_pToolBar->setStyleSheet("QToolBar {background-color: white;color:black;}");   //设置工具栏样式表
    m_pToolBar->setFixedSize(200, 40);  //设置工具栏固定大小
    m_pToolBar->hide();
}

void WidgetTest4::paintEvent(QPaintEvent* event)
{
    QPainter painter(this);
    QPen pen(QColor(0, 0, 255));
    painter.setPen(pen);
    if (m_bPress == true)
    {
        //画矩形上的点
        QBrush brush(QColor(0, 0, 255));
        painter.setBrush(brush);
        painter.drawRect(m_ptFrist.x()-2, m_ptFrist.y()-2, 5, 5);   
        painter.drawRect(m_ptFrist.x()+(m_ptEnd.x() - m_ptFrist.x())/2-2, m_ptFrist.y()-2, 5, 5);
        painter.drawRect(m_ptEnd.x()-2, m_ptFrist.y()-2, 5, 5);
        painter.drawRect(m_ptFrist.x()-2, m_ptFrist.y()+(m_ptEnd.y()-m_ptFrist.y())/2-2, 5, 5);
        painter.drawRect(m_ptEnd.x()-2, m_ptFrist.y()+(m_ptEnd.y() - m_ptFrist.y()) / 2-2, 5, 5);
        painter.drawRect(m_ptFrist.x()-2, m_ptEnd.y()-2, 5, 5);
        painter.drawRect(m_ptFrist.x()+(m_ptEnd.x() - m_ptFrist.x()) / 2-2, m_ptEnd.y()-2, 5, 5);
        painter.drawRect(m_ptEnd.x()-2, m_ptEnd.y()-2, 5, 5);

        //画矩形
        QBrush brush1(Qt::NoBrush);     
        painter.setBrush(brush1);
        painter.drawRect(m_ptFrist.x(), m_ptFrist.y(), m_ptEnd.x() - m_ptFrist.x(), m_ptEnd.y() - m_ptFrist.y());   //鼠标移动画矩形   
        painter.setClipRect(m_ptFrist.x(), m_ptFrist.y(), m_ptEnd.x() - m_ptFrist.x(), m_ptEnd.y() - m_ptFrist.y());
    }
}

void WidgetTest4::mouseMoveEvent(QMouseEvent* event)
{
    if (event->buttons() & Qt::LeftButton)  //左键按下移动
    {
        m_ptEnd = event->pos();
        int iWidth = m_ptEnd.x() - m_ptFrist.x();   //截取的宽度
        int iHeight = m_ptEnd.y() - m_ptFrist.y();  //截取的高度
        QString str = QString::number(abs(iWidth)) + "x" + QString::number(abs(iHeight));
        m_pLabel->setText(str); //鼠标移动实时显示大小
        m_pLabel->show();
        update();
    }
}

void WidgetTest4::mousePressEvent(QMouseEvent* event)
{
    if (event->button() == Qt::LeftButton)  //左键按下
    {
        m_bPress = true;
        m_ptFrist = event->pos();   //记录左键点击的坐标

        if (m_ptFrist.x() < 80 || m_ptFrist.y() < 30)
        {
            m_pLabel->setGeometry(m_ptFrist.x(), m_ptFrist.y(), 80, 30);    //设置label的位置及大小
        }
        else
        {
            m_pLabel->setGeometry(m_ptFrist.x(), m_ptFrist.y()-30, 80, 30); //设置label的位置及大小
        }
    }
}

void WidgetTest4::keyPressEvent(QKeyEvent* event)
{
    if ((event->modifiers() == Qt::ControlModifier) && (event->key() == Qt::Key_Q)) //组合键Ctrl+A
    {
        this->show();
        //设置主程序界面图片(截取主屏幕图片)
        QString strPath = QCoreApplication::applicationDirPath();
        QScreen* screen = QGuiApplication::primaryScreen();
        screen->grabWindow(0).save(strPath + "/temp.bmp");
        QPalette palette = this->palette();
        QImage bkImage(strPath + "/temp.bmp");
        QImage fillBkImage = bkImage.scaled(this->width(), this->height(), Qt::IgnoreAspectRatio);
        palette.setBrush(QPalette::Window, QBrush(fillBkImage));
        this->setPalette(palette);
    }
    else if (event->key() == Qt::Key_Escape) {
        close();    //关闭窗口
    }
}

void WidgetTest4::mouseReleaseEvent(QMouseEvent* event)
{
    if (event->button() == Qt::LeftButton)
    {
        m_pToolBar->setGeometry(m_ptEnd.x()-200, m_ptEnd.y(), m_ptEnd.x(), 30); //设置工具栏在指定位置
        m_pToolBar->show(); //显示工具栏
    }
}

void WidgetTest4::saveFile()
{
    QString strFile = QFileDialog::getSaveFileName(this, "save", "/1", "*.png;;*.jpg;;*.bmp");  //保存文件对话框
    //截取制定位置的图片
    QScreen *pScreen = QGuiApplication::primaryScreen();
    QPixmap pixmap = pScreen->grabWindow(0, m_ptFrist.x(), m_ptFrist.y(), m_ptEnd.x() - m_ptFrist.x(), m_ptEnd.y() - m_ptFrist.y());
    pixmap.save(strFile, "png");    //保存图片(格式:png,jpg,bmp)
}

void WidgetTest4::quit()
{
    m_pLabel->hide();   //隐藏坐标
    m_pToolBar->hide(); //隐藏工具栏
    m_bPress = false;   //鼠标弹起
    update();           //更新界面
}

void WidgetTest4::finish()
{
    //截取制定位置的图片
    QScreen* pScreen = QGuiApplication::primaryScreen();
    QPixmap pixmap = pScreen->grabWindow(0, m_ptFrist.x(), m_ptFrist.y(), m_ptEnd.x() - m_ptFrist.x(), m_ptEnd.y() - m_ptFrist.y());
    QClipboard* pClipboard = QApplication::clipboard();   //获取系统剪贴板指针
    pClipboard->setPixmap(pixmap);  //复制到剪切板
    close();
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,544评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,430评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,764评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,193评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,216评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,182评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,063评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,917评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,329评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,543评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,722评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,425评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,019评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,671评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,825评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,729评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,614评论 2 353

推荐阅读更多精彩内容