Qt实现最简单的截图工具

3 篇文章 0 订阅

ShotScreenDialog.h

#ifndef SHOTDIALOG_H
#define SHOTDIALOG_H
#include <QPixmap>
#include <QDialog>
#include <QToolBar>
#include <QPushButton>




namespace Ui {
class ShotScreenDialog;
}




class ShotRect : public QWidget
{
    Q_OBJECT
public:

    ShotRect(QWidget *parent = nullptr);
    void setScrPix(const QPixmap &pix);
    QPixmap getScrPix();
    QPixmap getShotPix();

protected:
    void paintEvent(QPaintEvent *e);
private:
    QPixmap m_scrPix;



};


class ShotScreenDialog : public QDialog
{
    Q_OBJECT
public:
    ShotScreenDialog(QWidget *parent = nullptr);
    QPixmap getShotPix();
protected:
    void paintEvent(QPaintEvent *e);
    void mousePressEvent(QMouseEvent *e);
    void mouseReleaseEvent(QMouseEvent *e);
    void mouseMoveEvent(QMouseEvent *e);
    bool eventFilter(QObject *o, QEvent *e);

private slots:
    void onBtnClicked();

private:
    void updateDargButtonsPosition();
    QRect getPostiveRectBaseOn2Point(QPoint pt0, QPoint pt1);
    QList<QPushButton *> getDragButtonsLst();
    void showToolBar();
    void hideToolBar();
    void setDragBtnCursor();
    void initStyleSheet();

private:
    bool m_mousePressed;
    QPoint m_mousePressedPoint;
    ShotRect *m_pShotRect;

    QPushButton *m_pTopLeftBtn;
    QPushButton *m_pTopCenterBtn;
    QPushButton *m_pTopRightBtn;

    QPushButton *m_pLeftBtn;
    QPushButton *m_pRightBtn;

    QPushButton *m_pBottomLeftBtn;
    QPushButton *m_pBottomCenterBtn;
    QPushButton *m_pBottomRightBtn;

    QRect m_oriShotRt;
    QPushButton *m_pDragBtnPressed;

private:
    Ui::ShotScreenDialog *ui;
};

#endif // SHOTDIALOG_H

ShotScreenDialog.cpp

#include "ShotScreenDialog.h"
#include <QScreen>
#include <QGuiApplication>
#include <QPainter>
#include <QDebug>
#include <QMouseEvent>
#include <QLabel>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QButtonGroup>
#include <qmath.h>
#include <QMouseEvent>
#include <QElapsedTimer>

ShotRect::ShotRect(QWidget *parent):QWidget(parent)
{

}
void ShotRect::setScrPix(const QPixmap &pix)
{
    m_scrPix = pix;
}
QPixmap ShotRect::getScrPix()
{
    return m_scrPix;
}
QPixmap ShotRect::getShotPix()
{
    return m_scrPix.copy(this->geometry());
}
void ShotRect::paintEvent(QPaintEvent *e)
{
    QPainter p(this);
    p.drawPixmap(1,1,this->width()-2, this->height()-2, m_scrPix.copy(pos().x()+1,pos().y()+1,rect().width()-2,rect().height()-2));
    QColor color = Qt::red;
    QPen pen;
    pen.setColor(color);
    pen.setWidth(1);
    p.setPen(pen);
    p.drawRect(0,0,this->width()-1, this->height()-1);
    return QWidget::paintEvent(e);
}




//
#include "ui_ShotScreenDialog.h"
ShotScreenDialog::ShotScreenDialog(QWidget *parent):
    QDialog(parent)
  , m_mousePressed(false)
  , m_mousePressedPoint(-1,-1)
  , m_pDragBtnPressed(nullptr)
  , ui(new Ui::ShotScreenDialog())
{
    ui->setupUi(this);
    ui->toolBar->hide();
    QScreen *screen = QGuiApplication::primaryScreen();
    

    m_pShotRect = new ShotRect(this);
    m_pShotRect->installEventFilter(this);
    m_pShotRect->setScrPix(screen->grabWindow(0));
	m_pShotRect->hide();

    m_pTopLeftBtn = new QPushButton(this);
    m_pTopCenterBtn = new QPushButton(this);
    m_pTopRightBtn = new QPushButton(this);

    m_pLeftBtn = new QPushButton(this);
    m_pRightBtn = new QPushButton(this);

    m_pBottomLeftBtn = new QPushButton(this);
    m_pBottomCenterBtn = new QPushButton(this);
    m_pBottomRightBtn = new QPushButton(this);


    QList<QPushButton *> lst = getDragButtonsLst();
    for(int i=0; i<lst.size(); ++i)
    {
        lst.at(i)->installEventFilter(this);
        lst.at(i)->setFixedSize(5,5);
    }

    setDragBtnCursor();

    connect(ui->okBtn, &QPushButton::clicked, this, &ShotScreenDialog::onBtnClicked);
    connect(ui->cancelBtn, &QPushButton::clicked, this, &ShotScreenDialog::onBtnClicked);

    initStyleSheet();
	showFullScreen();
}

QPixmap ShotScreenDialog::getShotPix()
{
    return m_pShotRect->getShotPix();
}

void ShotScreenDialog::paintEvent(QPaintEvent *e)
{
    QPixmap bgPix = m_pShotRect->getScrPix();
    QPixmap maskPix(bgPix.width(), bgPix.height());
    maskPix.fill((QColor(0, 0, 0, 180)));
    QPainter p(&bgPix);
    p.drawPixmap(0, 0, maskPix);
    QPainter pter(this);
    pter.drawPixmap(0,0,bgPix.width(), bgPix.height(), bgPix);

    QDialog::paintEvent(e);
}

void ShotScreenDialog::mousePressEvent(QMouseEvent *e)
{
    if(e->button() == Qt::LeftButton)
    {
        m_mousePressed = true;
        hideToolBar();
        m_mousePressedPoint = e->pos();

        int a = 5;
        QList<QPushButton *> lst = getDragButtonsLst();
        for(int i=0; i<lst.size(); ++i)
        {
            lst.at(i)->move(m_mousePressedPoint.x()-a/2, m_mousePressedPoint.y()-a/2);
            lst.at(i)->show();
        }

        m_pShotRect->setGeometry(m_mousePressedPoint.x(),m_mousePressedPoint.y(),0,0);
        m_pShotRect->show();

    }

    return QDialog::mousePressEvent(e);
}

void ShotScreenDialog::mouseReleaseEvent(QMouseEvent *e)
{
    m_mousePressedPoint = QPoint(-1,-1);
    m_mousePressed = false;
    showToolBar();

    if(e->buttons() == Qt::LeftButton)
    {
    }
    return QDialog::mouseReleaseEvent(e);
}


void ShotScreenDialog::mouseMoveEvent(QMouseEvent *e)
{
    QPoint dXY = e->pos() - m_mousePressedPoint;
    QPoint pt1;
    QPoint pt2;

    //生成矩形
    if(m_mousePressed)
    {
        pt1 = m_mousePressedPoint;
        pt2 = e->pos();
    }

    //微调矩形
    else if(m_pDragBtnPressed == m_pTopLeftBtn)
    {
        pt1 = m_oriShotRt.bottomRight();
        pt2 = m_oriShotRt.topLeft()+dXY;
    }
    else if(m_pDragBtnPressed == m_pTopCenterBtn)
    {
        pt1 = m_oriShotRt.bottomRight();
        pt2 = QPoint(m_oriShotRt.topLeft().x(), m_oriShotRt.topLeft().y() + dXY.y());
    }
    else if(m_pDragBtnPressed == m_pTopRightBtn)
    {
        pt1 = m_oriShotRt.bottomLeft();
        pt2 = m_oriShotRt.topRight()+dXY;
    }
    else if(m_pDragBtnPressed == m_pLeftBtn)
    {
        pt1 = m_oriShotRt.bottomRight();
        pt2 = QPoint(m_oriShotRt.topLeft().x() + dXY.x(),m_oriShotRt.topLeft().y());
    }
    else if(m_pDragBtnPressed == m_pRightBtn)
    {
        pt1 = m_oriShotRt.topLeft();
        pt2= QPoint(m_oriShotRt.bottomRight().x() + dXY.x(), m_oriShotRt.bottomRight().y());
    }
    else if(m_pDragBtnPressed == m_pBottomLeftBtn)
    {
        pt1 = m_oriShotRt.topRight();
        pt2 = m_oriShotRt.bottomLeft() + dXY;
    }
    else if(m_pDragBtnPressed == m_pBottomCenterBtn)
    {
        pt1 = m_oriShotRt.topLeft();
        pt2 = QPoint(m_oriShotRt.bottomRight().x(), m_oriShotRt.bottomRight().y() + dXY.y());
    }
    else if(m_pDragBtnPressed == m_pBottomRightBtn)
    {
        pt1 = m_oriShotRt.topLeft();
        pt2 = m_oriShotRt.bottomRight() + dXY;
    }

    QRect rt = getPostiveRectBaseOn2Point(pt1, pt2);
    m_pShotRect->setGeometry(rt);
    updateDargButtonsPosition();
    return QDialog::mouseMoveEvent(e);
}

bool ShotScreenDialog::eventFilter(QObject *o, QEvent *e)
{
    static bool shotRtPressed = false;
    static QPoint shotRtPressedPoint = QPoint(0,0);
    if(o == m_pShotRect)
    {
        if(e->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent *me = (QMouseEvent *)e;
            if(me->button() == Qt::LeftButton)
            {
                shotRtPressed = true;
                shotRtPressedPoint = me->pos();
                hideToolBar();
                return true;
            }
        }
        else if(e->type() == QEvent::MouseButtonRelease)
        {
            QMouseEvent *me = (QMouseEvent *)e;
            if(me->button() == Qt::LeftButton)
            {
                shotRtPressed = false;
                shotRtPressedPoint = QPoint(0,0);
                showToolBar();
                return true;
            }
        }
        else if(e->type() == QEvent::MouseMove)
        {
            if(shotRtPressed)
            {
                QMouseEvent *me = (QMouseEvent *)e;
                QPoint dXY = m_pShotRect->mapToParent(me->pos())- m_pShotRect->mapToParent(shotRtPressedPoint);
                m_pShotRect->move(m_pShotRect->pos() + dXY);
                updateDargButtonsPosition();
                return true;
            }
        }
    }
    
    QList<QPushButton *> lst = getDragButtonsLst();
    QPushButton *btn = (QPushButton *)o;
    if(lst.contains(btn))
    {
        if(e->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent *me = (QMouseEvent *)(e);
            if(me->button() == Qt::LeftButton)
            {
                m_pDragBtnPressed = qobject_cast<QPushButton *>(o);
                m_mousePressedPoint = m_pDragBtnPressed->mapTo(this, me->pos());
                m_oriShotRt = m_pShotRect->geometry();
                hideToolBar();
                return true;
            }
        }
        else if(e->type() == QEvent::MouseButtonRelease)
        {
            QMouseEvent *me = (QMouseEvent *)(e);
            if(me->button() == Qt::LeftButton)
            {
                m_pDragBtnPressed = nullptr;
                m_mousePressedPoint = QPoint(-1,-1);
                showToolBar();
                return true;
            }
        }
    }
    return QDialog::eventFilter(o, e);
}



void ShotScreenDialog::updateDargButtonsPosition()
{
    QRect rt = m_pShotRect->geometry();
    int a = 5;
    m_pTopLeftBtn->move(rt.topLeft().x()-a/2, rt.topLeft().y()-a/2);
    m_pTopCenterBtn->move((rt.topLeft().x()+rt.topRight().x())/2-a/2, rt.topLeft().y()-a/2);
    m_pTopRightBtn->move(rt.topRight().x()-a/2, rt.topRight().y()-a/2);

    m_pLeftBtn->move(rt.topLeft().x()-a/2, (rt.topLeft().y() + rt.bottomLeft().y())/2-a/2);
    m_pRightBtn->move(rt.topRight().x()-a/2, (rt.topRight().y() + rt.bottomLeft().y())/2-a/2);

    m_pBottomLeftBtn->move(rt.bottomLeft().x()-a/2, rt.bottomLeft().y()-a/2);
    m_pBottomCenterBtn->move((rt.bottomLeft().x() + rt.bottomRight().x())/2-a/2, rt.bottomLeft().y()-a/2);
    m_pBottomRightBtn->move(rt.bottomRight().x()-a/2,rt.bottomRight().y()-a/2);

}

QRect ShotScreenDialog::getPostiveRectBaseOn2Point(QPoint pt0, QPoint pt1)
{
    int mx = pt0.x();
    int my = pt0.y();
    int ex = pt1.x();
    int ey = pt1.y();
    QRect rt;
    if(mx < ex && my < ey)
    {
        rt = QRect(mx, my, ex-mx, ey-my);
    }
    else if(mx > ex && my < ey)
    {
        rt = QRect(ex, my, mx-ex, ey-my);
    }
    else if(mx < ex && my > ey)
    {
        rt = QRect(mx, ey, ex-mx, my-ey);
    }
    else if(mx > ex && my > ey)
    {
        rt = QRect(ex, ey, mx-ex, my-ey);
    }
    return rt;
}

QList<QPushButton *> ShotScreenDialog::getDragButtonsLst()
{
    QList<QPushButton *> lst;
    lst << m_pTopLeftBtn << m_pTopCenterBtn << m_pTopRightBtn
        << m_pLeftBtn << m_pRightBtn
        << m_pBottomLeftBtn << m_pBottomCenterBtn << m_pBottomRightBtn;

    return lst;
}

void ShotScreenDialog::showToolBar()
{
    ui->toolBar->setGeometry(m_pShotRect->geometry().bottomRight().x()- ui->toolBar->width()-3 ,m_pShotRect->geometry().bottomRight().y()+3
                             , 120, 30);
    if(m_pShotRect->geometry().width() > 6 &&  m_pShotRect->geometry().height() > 6)
        ui->toolBar->show();
    else
        ui->toolBar->hide();
}

void ShotScreenDialog::hideToolBar()
{
    ui->toolBar->hide();
}

void ShotScreenDialog::setDragBtnCursor()
{
    m_pTopLeftBtn->setCursor(Qt::SizeFDiagCursor);
    m_pTopCenterBtn->setCursor(Qt::SizeVerCursor);
    m_pTopRightBtn->setCursor(Qt::SizeBDiagCursor);

    m_pLeftBtn->setCursor(Qt::SizeHorCursor);
    m_pRightBtn->setCursor(Qt::SizeHorCursor);

    m_pBottomLeftBtn->setCursor(Qt::SizeBDiagCursor);
    m_pBottomCenterBtn->setCursor(Qt::SizeVerCursor);
    m_pBottomRightBtn->setCursor(Qt::SizeFDiagCursor);

    m_pShotRect->setCursor(Qt::SizeAllCursor);
}

void ShotScreenDialog::onBtnClicked()
{
    if(sender() == ui->okBtn)
    {
        this->reject();
    }
    else if(sender() == ui->cancelBtn)
    {
        this->accept();
    }
}

void ShotScreenDialog::initStyleSheet()
{
    setStyleSheet("QPushButton{border:none; background:red}");
	//ui->toolBar->setStyleSheet(".QWidget{background:rgba(0,0,0,50)}");
	ui->cancelBtn->setStyleSheet("QPushButton{background:white;color:#343434}");
    ui->okBtn->setStyleSheet("QPushButton{background:white;color:#343434}");
}


使用方法

 connect(ui->pushButton, &QPushButton::clicked, [this]()
    {
        this->hide();
        ShotScreenDialog dlg;
        if(dlg.exec() == QDialog::Accepted)
        {
            ui->label->setPixmap(dlg.getShotPix());
        }
        this->show();
    });

下载地址:https://download.csdn.net/download/qq_22302267/12373634

  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值