Qt实现截图之三 截图裁剪框实现方案2 绘制方式

QT实现头像图片剪切框 近期在做一个qt项目,在用户选择头像图片后,需要将图片载入,并对其进行用户自定义裁剪。通过研究参照各流行软件的裁剪方式后,发现qq实现的裁剪比较好看,于是,我想那就做一个和qq相似的吧。先放一张qq实现的效果,然后最后再放我实现的效果。 1. 怎样去实现裁剪 对于一张载入的图片,要实现用户自定义裁剪,那么首先我需要有一个能够响应用户自由缩放的边框,当用户缩放到心仪大小,再摆到适当位置, 阅读详情

前面介绍了一种裁剪框的实现方案详见Qt实现截图之二 ,本节再介绍一种实现方案。

分层依旧两层,改变的是裁剪层,差别在于只需要一个裁剪窗口也就是去掉前面的top,right, left,bottom,只留下mid窗体用于高亮显示裁剪区域。

布局如下:

void MaskFrameBase::setup()
{
    this->setMouseTracking(true);

   // this->setObjectName("shtcenterframe");
    this->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

    this->captureArea = new QFrame(this);
    this->captureArea->installEventFilter(this);
    this->captureArea->setObjectName("captureareaframe");
    this->captureArea->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    this->captureArea->setMouseTracking(true);
    createBtnFrame();

    bSetupView = true;

    adjustCaptureArea();
    this->captureArea->resize(0, 0);
    this->captureArea->setVisible(false);
}

1.绘制裁剪框

首先计算一下边框绘制的区域


void MaskFrameBase::calRect()
{
    leftTopRect = QRect(edgeMargin, edgeMargin, _rect_border_size.width(), _rect_border_size.height());
    topRect = QRect((this->captureArea->width() - _rect_border_size.width()) * 0.5, edgeMargin, _rect_border_size.width(), _rect_border_size.height());
    rigtTopRect = QRect(this->captureArea->width() - _rect_border_size.width() - edgeMargin, edgeMargin, _rect_border_size.width(), _rect_border_size.height());
    rightRect = QRect(this->captureArea->width() - _rect_border_size.width() - edgeMargin, (this->captureArea->height() - _rect_border_size.height()) * 0.5, _rect_border_size.width(), _rect_border_size.height());
    rightBottomRect = QRect(this->captureArea->width() - _rect_border_size.width() - edgeMargin, this->captureArea->height() - _rect_border_size.height() - edgeMargin, _rect_border_size.width(), _rect_border_size.height());
    bottomRect = QRect((this->captureArea->width() - _rect_border_size.width()) * 0.5, this->captureArea->height() - _rect_border_size.height() - edgeMargin, _rect_border_size.width(), _rect_border_size.height());
    leftBottomRect = QRect(edgeMargin, this->captureArea->height() - _rect_border_size.height() - edgeMargin, _rect_border_size.width(), _rect_border_size.height());
    leftRect = QRect(edgeMargin, (this->captureArea->height() - _rect_border_size.height()) * 0.5, _rect_border_size.width(), _rect_border_size.height());
    centerRect = QRect(_rect_border_size.width() * 0.5 + edgeMargin, _rect_border_size.height() * 0.5 + edgeMargin, this->captureArea->width() - _rect_border_size.width() - edgeMargin * 2, this->captureArea->height() - _rect_border_size.height()- edgeMargin*2);
}

 

绘制跟之前一样

bool MaskFrameBase::eventFilter(QObject *obj, QEvent *event)
{
    if(obj == this->captureArea)
    {
        if(event->type() == QEvent::Paint)
        {
            QPainter painter(captureArea);

            painter.setRenderHint(QPainter::Antialiasing, true);
            painter.setPen(QPen(QColor(0xFC, 0xC9, 0x01), lineWidth,  Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));    //设置画笔形式
            //画矩形
            calRect();

            QPainterPath path;
            path.addEllipse(leftRect.left(), leftRect.top(), leftRect.width(), leftRect.width());
            path.addEllipse(topRect.left(), topRect.top(), topRect.width(), topRect.width());
            path.addEllipse(rigtTopRect.left(), rigtTopRect.top(), rigtTopRect.width(), rigtTopRect.width());
            path.addEllipse(rightRect.left(), rightRect.top(), rightRect.width(), rightRect.width());
            path.addEllipse(rightBottomRect.left(), rightBottomRect.top(), rightBottomRect.width(), rightBottomRect.width());
            path.addEllipse(bottomRect.left(), bottomRect.top(), bottomRect.width(), bottomRect.width());
            path.addEllipse(leftBottomRect.left(), leftBottomRect.top(), leftBottomRect.width(), leftBottomRect.width());
            path.addEllipse(leftTopRect.left(), leftTopRect.top(), leftTopRect.width(), leftTopRect.width());
            painter.setBrush(Qt::white);
            painter.drawPath(path);

            //划线
            painter.drawLine(QLine(QPoint(_rect_border_size.width() + edgeMargin, _rect_border_size.height() * 0.5 + edgeMargin), QPoint((this->captureArea->width() - _rect_border_size.width()) * 0.5 - 1, _rect_border_size.height() * 0.5 + edgeMargin)));
            painter.drawLine(QLine(QPoint((this->captureArea->width() - _rect_border_size.width()) * 0.5 + _rect_border_size.width(), _rect_border_size.height() * 0.5 + edgeMargin), QPoint(this->captureArea->width() - _rect_border_size.width() - edgeMargin - 1, _rect_border_size.height() * 0.5 + edgeMargin)));

            painter.drawLine(QLine(QPoint(this->captureArea->width() - _rect_border_size.width() * 0.5 - edgeMargin, _rect_border_size.height() + edgeMargin), QPoint(this->captureArea->width() - _rect_border_size.width() * 0.5 - edgeMargin, (this->captureArea->height() - _rect_border_size.height()) * 0.5 - 1)));
            painter.drawLine(QLine(QPoint(this->captureArea->width() - _rect_border_size.width() * 0.5 - edgeMargin, (this->captureArea->height() - _rect_border_size.height()) * 0.5 + _rect_border_size.height()), QPoint(this->captureArea->width() - _rect_border_size.width() * 0.5 - edgeMargin, this->captureArea->height() - _rect_border_size.height() - edgeMargin - 1)));

            painter.drawLine(QLine(QPoint((this->captureArea->width() - _rect_border_size.width()) * 0.5 + _rect_border_size.width(), this->captureArea->height() - _rect_border_size.height() * 0.5 - edgeMargin), QPoint(this->captureArea->width() - _rect_border_size.width() - edgeMargin - 1, this->captureArea->height() - _rect_border_size.height() * 0.5 - edgeMargin)));
            painter.drawLine(QLine(QPoint(_rect_border_size.width() + edgeMargin, this->captureArea->height() - _rect_border_size.height() * 0.5 - edgeMargin), QPoint((this->captureArea->width() - _rect_border_size.width()) * 0.5 - 1, this->captureArea->height() - _rect_border_size.height() * 0.5 - edgeMargin)));

            painter.drawLine(QLine(QPoint(_rect_border_size.width() * 0.5 + edgeMargin, _rect_border_size.height() + edgeMargin), QPoint(_rect_border_size.width() * 0.5 + edgeMargin, (this->captureArea->height() - _rect_border_size.height()) * 0.5 - 1)));
            painter.drawLine(QLine(QPoint(_rect_border_size.width() * 0.5 + edgeMargin, (this->captureArea->height() - _rect_border_size.height()) * 0.5 + _rect_border_size.height()), QPoint(_rect_border_size.width() * 0.5 + edgeMargin, this->captureArea->height() - _rect_border_size.height() - edgeMargin - 1)));
        }
    }
    return QFrame::eventFilter(obj, event);
}

2.重载父窗口paintEvent 

重载这个事件是为了绘制非裁减区域,绘制层半透明,首先要判断一下裁剪区域是否显示且有效,无效则全屏绘制成半透明,代码如下:

void MaskFrameBase::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    if(this->captureArea->isVisible() && captureAreaValide())
    {
        //左边区域
        painter.fillRect(QRect(0, this->captureArea->pos().y()+_rect_border_size.height() * 0.5+edgeMargin,
                               this->captureArea->pos().x()+_rect_border_size.width() * 0.5+edgeMargin ,
                               this->captureArea->height()-_rect_border_size.height()-edgeMargin*2),
                         QBrush(QColor(0, 0, 0,128)));
        //上边区域
        painter.fillRect(QRect(0, 0, width(), this->captureArea->pos().y()+_rect_border_size.height() * 0.5+edgeMargin),
                         QBrush(QColor(0, 0, 0,128)));
        //右边区域
        painter.fillRect(QRect(this->captureArea->geometry().right()-edgeMargin-_rect_border_size.width() * 0.5,
                               this->captureArea->pos().y()+_rect_border_size.height() * 0.5+edgeMargin,
                               width()-this->captureArea->geometry().right()+_rect_border_size.width() * 0.5+edgeMargin,
                               this->captureArea->height()-_rect_border_size.height()-edgeMargin*2),
                                QBrush(QColor(0, 0, 0,128)));
        //下边区域
         painter.fillRect(QRect(0,
                                this->captureArea->geometry().top()+this->captureArea->height()-_rect_border_size.height() * 0.5-edgeMargin,
                                width(),
                                height()-this->captureArea->geometry().bottom()+_rect_border_size.height()*0.5+edgeMargin),
                                QBrush(QColor(0, 0, 0,128)));

        //透明区域
        QRect rect;
        rect.setX(this->captureArea->pos().x()+_rect_border_size.width()*0.5+edgeMargin);
        rect.setY(this->captureArea->pos().y()+_rect_border_size.height()*0.5 +edgeMargin);
        rect.setWidth(this->captureArea->width()-_rect_border_size.width()-edgeMargin*2);
        rect.setHeight(this->captureArea->height()-_rect_border_size.height()-edgeMargin*2);
    #ifdef Q_OS_WIN
            painter.fillRect(rect, QBrush(QColor(0, 0, 0, 1)));
    #else
            painter.fillRect(rect, QBrush(QColor(0, 0, 0, 20)));
    #endif
    }
    else
    {
        painter.fillRect(rect(), QBrush(QColor(0, 0, 0, 128)));
    }

}

3.效果

效果跟方案一是一样的:

 

完整代码:

#include "maskframebase.h"
#include "remixhelp.h"
#include <QFrame>
#include <QLabel>
#include <QMouseEvent>
#include <QToolButton>
#include <QHBoxLayout>
#include <QApplication>
#include <QDesktopWidget>
#include <QDebug>
#include <QPainter>
#include <QPushButton>

#define SNAP_BTN_SAVE "snapsavebtn"
#define SNAP_BTN_CANCEL "snapcancelbtn"
#define SNAP_BTN_SEND "snapsendbtn"

MaskFrameBase::MaskFrameBase(QWidget* parent):QFrame(parent),captureArea(NULL),
    btnFrame(NULL),bSetupView(false),pressPoint(0,0),curmousePoint(0,0),
    startPointCapture(0,0),endPointCapture(0,0),operateMode(SelectArea),
    borderSide(LeftBorder),cornerSide(LeftTop),bLeftPressed(false),
    _rect_border_size(dpiSize(12, 12)),edgeMargin(dpiWidth(4)), lineWidth(dpiWidth(3))
{
    setFixedParas();
}

MaskFrameBase::~MaskFrameBase()
{
}

void MaskFrameBase::setFixedParas()
{
    this->mScale = 1;
    this->btnHeigh = 34*mScale;
    this->btnFrameHeigh = dpiHeight(70);
    this->btnFrameWidth = dpiWidth(220);
    this->sizeTipFrameWidth = 80*mScale;
    this->sizeTipFrameHeigh = 40*mScale;
    this->captureBorderWidth = _rect_border_size.width()*0.5+edgeMargin+lineWidth;
    this->borderEdgeWidth = 0;
    this->maskColor = "rgba(0, 0, 0 , 178)";//70%透明度
}

void MaskFrameBase::setup()
{
    this->setMouseTracking(true);

   // this->setObjectName("shtcenterframe");
    this->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

    this->captureArea = new QFrame(this);
    this->captureArea->installEventFilter(this);
    this->captureArea->setObjectName("captureareaframe");
    this->captureArea->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    this->captureArea->setMouseTracking(true);
    createBtnFrame();

    bSetupView = true;

    adjustCaptureArea();
    this->captureArea->resize(0, 0);
    this->captureArea->setVisible(false);
}

void MaskFrameBase::showMask()
{
    this->show();
    this->setCursor(Qt::CrossCursor);
    this->raise();
}

void MaskFrameBase::reset()
{
    this->pressPoint = QPoint(0,0);
    this->curmousePoint = QPoint(0,0);
    this->startPointCapture = QPoint(0,0);
    this->endPointCapture = QPoint(0,0);
    this->startPointCaptureMark = QPoint(0,0);//按下鼠标瞬间的截屏区域
    this->endPointCaptureMark = QPoint(0,0);
    this->operateMode = SelectArea;
    this->borderSide = LeftBorder;
    this->cornerSide = LeftTop;
    this->setCursor(Qt::CrossCursor);

    this->bLeftPressed = false;
    this->btnFrame->hide();
    adjustCaptureArea();
    this->hide();
}

void MaskFrameBase::resizeEvent(QResizeEvent* event)
{
    QFrame::resizeEvent(event);
    adjustCaptureArea();
}

void MaskFrameBase::mousePressEvent(QMouseEvent* event)
{
    QFrame::mousePressEvent(event);

    if(event->button() == Qt::RightButton)
    {
        if(event->type() == QEvent::MouseButtonPress)
        {
            //reset();
            //cancelSelect();
            //emit selectedSnap(QPoint(0,0),QPoint(0,0));
        }
    }
    else if(event->button() == Qt::LeftButton)
    {
        this->pressPoint = event->pos();
        this->bLeftPressed = true;

        this->startPointCaptureMark = this->startPointCapture;//按下鼠标瞬间的截屏区域
        this->endPointCaptureMark = this->endPointCapture;

        //qDebug()<<"mousePressEvent:bLeftPressed"<<endl;
    }
}

void MaskFrameBase::mouseMoveEvent(QMouseEvent *event)
{
    QFrame::mouseMoveEvent(event);
    this->curmousePoint = event->pos();

//    static int index = 0;
//    qDebug()<<"MaskFrameBase::mouseMoveEvent:"<<index<<endl;
//    ++index;
    restrictMouseMove(this->curmousePoint);

    if(this->bLeftPressed)
    {
        this->btnFrame->hide();

        if(updateCaptureRect())
        {
            adjustCaptureArea();
        }
    }

    if(!this->bLeftPressed){
        judgeOperateState(this->curmousePoint);
    }
}

void MaskFrameBase::restrictMouseMove(QPoint& pt)
{
    //qDebug()<<"MaskFrameBase::restrictMouseMove:x:"<<pt.x()<<",y:"<<pt.y()<<endl;

    bool outbounds = false;
    if(pt.x() <= 0){
        pt.setX(0);
        outbounds = true;
        //qDebug()<<"MaskFrameBase::restrictMouseMove:left"<<endl;
    }

    if(pt.x() >= this->width()){
        pt.setX(this->width());
        outbounds = true;
        //qDebug()<<"MaskFrameBase::restrictMouseMove:right"<<endl;
    }

    if(pt.y() <= 0){
        pt.setY(0);
        outbounds = true;
        //qDebug()<<"MaskFrameBase::restrictMouseMove:top"<<endl;
    }

    if(pt.y() >= this->height()){
        pt.setY(this->height());
        outbounds = true;
        //qDebug()<<"MaskFrameBase::restrictMouseMove:bottom"<<endl;
    }

    if(outbounds){
        //固定一下鼠标
        QApplication::desktop()->cursor().setPos(this->mapToGlobal(pt));
    }
}

void MaskFrameBase::mouseReleaseEvent(QMouseEvent *event)
{
    QFrame::mouseReleaseEvent(event);

    //qDebug()<<"MaskFrameBase::mouseReleaseEvent"<<endl;

    if(event->button() == Qt::LeftButton)
    {
        //qDebug()<<"MaskFrameBase::mouseReleaseEvent LeftButton"<<endl;

        this->bLeftPressed = false;

        this->curmousePoint = event->pos();

        //只点击一下的情况
        if(qAbs(this->curmousePoint.x()-this->pressPoint.x())<=this->captureBorderWidth &&
                qAbs(this->curmousePoint.y()-pressPoint.y())<=this->captureBorderWidth &&
                (this->operateMode==SelectArea || this->operateMode==Normal)){
            showBtnFrame();
            return;
        }

        adjustCaptureArea();
        showBtnFrame();
    }
}

void MaskFrameBase::enterEvent(QEvent *e)
{
    QFrame::enterEvent(e);
}

void MaskFrameBase::leaveEvent(QEvent *e)
{
    QFrame::leaveEvent(e);
 }


void MaskFrameBase::calRect()
{
    leftTopRect = QRect(edgeMargin, edgeMargin, _rect_border_size.width(), _rect_border_size.height());
    topRect = QRect((this->captureArea->width() - _rect_border_size.width()) * 0.5, edgeMargin, _rect_border_size.width(), _rect_border_size.height());
    rigtTopRect = QRect(this->captureArea->width() - _rect_border_size.width() - edgeMargin, edgeMargin, _rect_border_size.width(), _rect_border_size.height());
    rightRect = QRect(this->captureArea->width() - _rect_border_size.width() - edgeMargin, (this->captureArea->height() - _rect_border_size.height()) * 0.5, _rect_border_size.width(), _rect_border_size.height());
    rightBottomRect = QRect(this->captureArea->width() - _rect_border_size.width() - edgeMargin, this->captureArea->height() - _rect_border_size.height() - edgeMargin, _rect_border_size.width(), _rect_border_size.height());
    bottomRect = QRect((this->captureArea->width() - _rect_border_size.width()) * 0.5, this->captureArea->height() - _rect_border_size.height() - edgeMargin, _rect_border_size.width(), _rect_border_size.height());
    leftBottomRect = QRect(edgeMargin, this->captureArea->height() - _rect_border_size.height() - edgeMargin, _rect_border_size.width(), _rect_border_size.height());
    leftRect = QRect(edgeMargin, (this->captureArea->height() - _rect_border_size.height()) * 0.5, _rect_border_size.width(), _rect_border_size.height());
    centerRect = QRect(_rect_border_size.width() * 0.5 + edgeMargin, _rect_border_size.height() * 0.5 + edgeMargin, this->captureArea->width() - _rect_border_size.width() - edgeMargin * 2, this->captureArea->height() - _rect_border_size.height()- edgeMargin*2);
}

bool MaskFrameBase::eventFilter(QObject *obj, QEvent *event)
{
    if(obj == this->captureArea)
    {
        if(event->type() == QEvent::Paint)
        {
            QPainter painter(captureArea);

            painter.setRenderHint(QPainter::Antialiasing, true);
            painter.setPen(QPen(QColor(0xFC, 0xC9, 0x01), lineWidth,  Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));    //设置画笔形式
            //画矩形
            calRect();

            QPainterPath path;
            path.addEllipse(leftRect.left(), leftRect.top(), leftRect.width(), leftRect.width());
            path.addEllipse(topRect.left(), topRect.top(), topRect.width(), topRect.width());
            path.addEllipse(rigtTopRect.left(), rigtTopRect.top(), rigtTopRect.width(), rigtTopRect.width());
            path.addEllipse(rightRect.left(), rightRect.top(), rightRect.width(), rightRect.width());
            path.addEllipse(rightBottomRect.left(), rightBottomRect.top(), rightBottomRect.width(), rightBottomRect.width());
            path.addEllipse(bottomRect.left(), bottomRect.top(), bottomRect.width(), bottomRect.width());
            path.addEllipse(leftBottomRect.left(), leftBottomRect.top(), leftBottomRect.width(), leftBottomRect.width());
            path.addEllipse(leftTopRect.left(), leftTopRect.top(), leftTopRect.width(), leftTopRect.width());
            painter.setBrush(Qt::white);
            painter.drawPath(path);

            //划线
            painter.drawLine(QLine(QPoint(_rect_border_size.width() + edgeMargin, _rect_border_size.height() * 0.5 + edgeMargin), QPoint((this->captureArea->width() - _rect_border_size.width()) * 0.5 - 1, _rect_border_size.height() * 0.5 + edgeMargin)));
            painter.drawLine(QLine(QPoint((this->captureArea->width() - _rect_border_size.width()) * 0.5 + _rect_border_size.width(), _rect_border_size.height() * 0.5 + edgeMargin), QPoint(this->captureArea->width() - _rect_border_size.width() - edgeMargin - 1, _rect_border_size.height() * 0.5 + edgeMargin)));

            painter.drawLine(QLine(QPoint(this->captureArea->width() - _rect_border_size.width() * 0.5 - edgeMargin, _rect_border_size.height() + edgeMargin), QPoint(this->captureArea->width() - _rect_border_size.width() * 0.5 - edgeMargin, (this->captureArea->height() - _rect_border_size.height()) * 0.5 - 1)));
            painter.drawLine(QLine(QPoint(this->captureArea->width() - _rect_border_size.width() * 0.5 - edgeMargin, (this->captureArea->height() - _rect_border_size.height()) * 0.5 + _rect_border_size.height()), QPoint(this->captureArea->width() - _rect_border_size.width() * 0.5 - edgeMargin, this->captureArea->height() - _rect_border_size.height() - edgeMargin - 1)));

            painter.drawLine(QLine(QPoint((this->captureArea->width() - _rect_border_size.width()) * 0.5 + _rect_border_size.width(), this->captureArea->height() - _rect_border_size.height() * 0.5 - edgeMargin), QPoint(this->captureArea->width() - _rect_border_size.width() - edgeMargin - 1, this->captureArea->height() - _rect_border_size.height() * 0.5 - edgeMargin)));
            painter.drawLine(QLine(QPoint(_rect_border_size.width() + edgeMargin, this->captureArea->height() - _rect_border_size.height() * 0.5 - edgeMargin), QPoint((this->captureArea->width() - _rect_border_size.width()) * 0.5 - 1, this->captureArea->height() - _rect_border_size.height() * 0.5 - edgeMargin)));

            painter.drawLine(QLine(QPoint(_rect_border_size.width() * 0.5 + edgeMargin, _rect_border_size.height() + edgeMargin), QPoint(_rect_border_size.width() * 0.5 + edgeMargin, (this->captureArea->height() - _rect_border_size.height()) * 0.5 - 1)));
            painter.drawLine(QLine(QPoint(_rect_border_size.width() * 0.5 + edgeMargin, (this->captureArea->height() - _rect_border_size.height()) * 0.5 + _rect_border_size.height()), QPoint(_rect_border_size.width() * 0.5 + edgeMargin, this->captureArea->height() - _rect_border_size.height() - edgeMargin - 1)));
        }
        if(event->type() == QEvent::Resize || event->type() == QEvent::Move )
        {
            repaint();
        }
    }
    return QFrame::eventFilter(obj, event);
}

void MaskFrameBase::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    if(this->captureArea->isVisible() && captureAreaValide())
    {
        //左边区域
        painter.fillRect(QRect(0, this->captureArea->pos().y()+_rect_border_size.height() * 0.5+edgeMargin,
                               this->captureArea->pos().x()+_rect_border_size.width() * 0.5+edgeMargin ,
                               this->captureArea->height()-_rect_border_size.height()-edgeMargin*2),
                         QBrush(QColor(0, 0, 0,128)));
        //上边区域
        painter.fillRect(QRect(0, 0, width(), this->captureArea->pos().y()+_rect_border_size.height() * 0.5+edgeMargin),
                         QBrush(QColor(0, 0, 0,128)));
        //右边区域
        painter.fillRect(QRect(this->captureArea->geometry().right()-edgeMargin-_rect_border_size.width() * 0.5,
                               this->captureArea->pos().y()+_rect_border_size.height() * 0.5+edgeMargin,
                               width()-this->captureArea->geometry().right()+_rect_border_size.width() * 0.5+edgeMargin,
                               this->captureArea->height()-_rect_border_size.height()-edgeMargin*2),
                                QBrush(QColor(0, 0, 0,128)));
        //下边区域
         painter.fillRect(QRect(0,
                                this->captureArea->geometry().top()+this->captureArea->height()-_rect_border_size.height() * 0.5-edgeMargin,
                                width(),
                                height()-this->captureArea->geometry().bottom()+_rect_border_size.height()*0.5+edgeMargin),
                                QBrush(QColor(0, 0, 0,128)));

        //透明区域
        QRect rect;
        rect.setX(this->captureArea->pos().x()+_rect_border_size.width()*0.5+edgeMargin);
        rect.setY(this->captureArea->pos().y()+_rect_border_size.height()*0.5 +edgeMargin);
        rect.setWidth(this->captureArea->width()-_rect_border_size.width()-edgeMargin*2);
        rect.setHeight(this->captureArea->height()-_rect_border_size.height()-edgeMargin*2);
    #ifdef Q_OS_WIN
            painter.fillRect(rect, QBrush(QColor(0, 0, 0, 1)));
    #else
            painter.fillRect(rect, QBrush(QColor(0, 0, 0, 20)));
    #endif
    }
    else
    {
        painter.fillRect(rect(), QBrush(QColor(0, 0, 0, 128)));
    }

}

void MaskFrameBase::createBtnFrame()//按钮
{
}

void MaskFrameBase::cancelSelect()
{
}

void MaskFrameBase::adjustCaptureArea()
{
    if(this->width()==0 || this->height()==0){
        return;
    }
    int startx = this->startPointCapture.x();
    int starty = this->startPointCapture.y();
    int endx = this->endPointCapture.x();
    int endy = this->endPointCapture.y();

   int captureHeight = endy-starty;
    //mid-capture
    int captureWidth = endx-startx;
    this->captureArea->setGeometry(startx,starty,captureWidth,captureHeight);
    if(!this->captureArea->isVisible()){
        this->captureArea->show();
    }
}

//只处理公共的移动事件,在移动时,调整抓取的区域坐标
bool MaskFrameBase::updateCaptureRect()
{
    if(this->operateMode == MoveArea)
    {
        if(captureAreaValide()){
            updateCaptureRectMove();
            return true;
        }
    }

    return false;
}

//移动时,改变抓屏区坐标
void MaskFrameBase::updateCaptureRectMove()
{
    int movex = this->curmousePoint.x()-this->pressPoint.x();
    int movey = this->curmousePoint.y()-this->pressPoint.y();

    //qDebug()<<"updateCaptureRectMove:"<<movex<<" "<<movey<<endl;

    bool outbounds = false;
    if(movex > 0){
        //移动到右边界时,停止移动
        if(this->endPointCaptureMark.x()+movex > this->width()){
            //qDebug()<<"updateCaptureRectMove over right"<<endl;
            movex = this->width()-this->endPointCaptureMark.x();
            outbounds = true;
        }
    }
    else{
        //移动到左边界
        if(this->startPointCaptureMark.x()+movex < 0){
            //qDebug()<<"updateCaptureRectMove over left"<<endl;
            movex = -this->startPointCaptureMark.x();
            outbounds = true;
        }
    }

    if(movey > 0){
        //移动到下边界
        if(this->endPointCaptureMark.y()+movey > this->height()){
            //qDebug()<<"updateCaptureRectMove over bottom"<<endl;
            movey = this->height()-this->endPointCaptureMark.y();
            outbounds = true;
        }
    }
    else{
        //移动到上边界
        if(this->startPointCaptureMark.y()+movey < 0){
            //qDebug()<<"updateCaptureRectMove over top"<<endl;
            movey = -this->startPointCaptureMark.y();
            outbounds = true;
        }
    }

    if(outbounds){
        //固定一下鼠标
        QPoint curcurpos(this->pressPoint.x()+movex,this->pressPoint.y()+movey);
        QApplication::desktop()->cursor().setPos(this->mapToGlobal(curcurpos));
    }

    //调整截屏区域坐标
    this->startPointCapture.setX(this->startPointCaptureMark.x()+movex);
    this->startPointCapture.setY(this->startPointCaptureMark.y()+movey);
    this->endPointCapture.setX(this->endPointCaptureMark.x()+movex);
    this->endPointCapture.setY(this->endPointCaptureMark.y()+movey);
}

void MaskFrameBase::alignPoint(QPoint& startpt,QPoint& endpt)
{
    int startx = startpt.x();
    int starty = startpt.y();
    int endx = endpt.x();
    int endy = endpt.y();
    startpt.setX(qMin(startx,endx));
    startpt.setY(qMin(starty,endy));
    endpt.setX(qMax(startx,endx));
    endpt.setY(qMax(starty,endy));
}

void MaskFrameBase::generateResultPos(QPoint startpt,QPoint endpt)
{
    this->startPointCapture.setX(qMin(startpt.x(),endpt.x()));
    this->startPointCapture.setY(qMin(startpt.y(),endpt.y()));
    this->endPointCapture.setX(qMax(startpt.x(),endpt.x()));
    this->endPointCapture.setY(qMax(startpt.y(),endpt.y()));
}

void MaskFrameBase::showBtnFrame()
{
    if(this->btnFrame==NULL || !captureAreaValide()){
        return;
    }

    //右下角显示的
    int posX = this->endPointCapture.x()-this->btnFrameWidth;
    int posY = this->endPointCapture.y();

    if(posX <= 0){
        posX = this->startPointCapture.x();
    }

    if(posY+this->btnFrameHeigh >= this->height()){
        if(this->startPointCapture.y()-1-this->btnFrameHeigh > 0){
            posY = this->startPointCapture.y()-this->btnFrameHeigh;
        }
        else{
            posY = this->endPointCapture.y()-1-this->btnFrameHeigh;
        }
    }

//    //右上角显示的
//    int posX = this->endPointCapture.x()-this->btnFrameWidth;
//    int posY = this->startPointCapture.y()-this->btnFrameHeigh;

//    //截屏区宽度小于工具栏宽度,且截屏区很靠左侧
//    if(posX <= 0){
//        posX = this->startPointCapture.x();
//    }

//    if(posY <= 1){
//        if(this->endPointCapture.y()+this->btnFrameHeigh+1 < this->height()){
//            posY = this->endPointCapture.y();
//        }
//        else{
//            posY = this->startPointCapture.y()+1;
//        }
//    }

    this->btnFrame->setGeometry(posX,posY,this->btnFrameWidth,this->btnFrameHeigh);
    this->btnFrame->show();
    this->btnFrame->raise();
}

QPushButton* MaskFrameBase::createActionButton(const QString &name, const QString& toolTipKey, const QString &icon){
    QPushButton* actionButton = new QPushButton(this->btnFrame);
    actionButton->setIconSize(dpiSize(20, 20));
    actionButton->setIcon(QIcon(icon));
    actionButton->setText(name);
    actionButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    actionButton->setFixedSize(dpiSize(96, 50));

    return actionButton;
}

//鼠标未按下左键,移动过程中使用,会改变鼠标指针样式
bool MaskFrameBase::judgeOperateState(QPoint curpos)
{
    if(this->bLeftPressed){
        return false;
    }

    int tmpptx = this->startPointCapture.x()+this->captureBorderWidth+this->borderEdgeWidth;
    int tmppty = this->startPointCapture.y()+this->captureBorderWidth+this->borderEdgeWidth;
    QPoint startTmp(tmpptx,tmppty);
    tmpptx = this->endPointCapture.x()-this->captureBorderWidth-this->borderEdgeWidth;
    tmppty = this->endPointCapture.y()-this->captureBorderWidth-this->borderEdgeWidth;
    QPoint endTmp(tmpptx,tmppty);
    QRect captureRect(startTmp,endTmp);

    //无选定区域时,显示选中图
    if(captureRect.width()<captureBorderWidth*2 || captureRect.height()<captureBorderWidth*2){
          if(!judgeUponCorner(curpos))
          {
              if(captureRect.contains(curpos)){
                  this->operateMode = MoveArea;
                  this->setCursor(Qt::SizeAllCursor);
                  //qDebug()<<"judgeOperateState MoveArea"<<endl;

              }
              else
              {
                  this->operateMode = SelectArea;
                  this->setCursor(Qt::CrossCursor);
              }
          }            

        return true;
    }

    //是否在弹出按钮上
    if(judgeUponBtnFrame(curpos)){
        //qDebug()<<"judgeOperateState onBotton"<<endl;
        return true;
    }

    //在截取区,可拖动选取区域
    if(captureRect.contains(curpos)){
        this->operateMode = MoveArea;
        this->setCursor(Qt::SizeAllCursor);
        //qDebug()<<"judgeOperateState MoveArea"<<endl;
        return true;
    }

    //判断是否在边框上
    if(judgeUponBorder(curpos)){
        //qDebug()<<"judgeOperateState:border match"<<endl;
        return true;
    }

    return false;

    //在四个边角的判断不是每个子类都需要

    //qDebug()<<"judgeOperateState last SelectArea"<<endl;
}

//鼠标是否移动到四个边角
bool MaskFrameBase::judgeUponCorner(QPoint curpos)
{
    if(this->bLeftPressed){
        return false;
    }

    //左上角
    int tmpptx = this->startPointCapture.x()-this->borderEdgeWidth;
    int tmppty = this->startPointCapture.y()-this->borderEdgeWidth;
    QPoint startTmp(tmpptx,tmppty);
    tmpptx = this->startPointCapture.x()+this->captureBorderWidth+this->borderEdgeWidth;
    tmppty = this->startPointCapture.y()+this->captureBorderWidth+this->borderEdgeWidth;
    QPoint endTmp(tmpptx,tmppty);
    QRect leftTopRect(startTmp,endTmp);
    if(leftTopRect.width()>1 && leftTopRect.height()>1 &&
            leftTopRect.contains(curpos)){
        this->operateMode = CornerScale;
        this->cornerSide = LeftTop;
        this->setCursor(Qt::SizeFDiagCursor);

        //qDebug()<<"judgeUponCorner LeftTop"<<endl;

        return true;
    }

    //右上角
    tmpptx = this->endPointCapture.x()-this->captureBorderWidth-this->borderEdgeWidth;
    tmppty = this->startPointCapture.y()-this->borderEdgeWidth;
    startTmp.setX(tmpptx);
    startTmp.setY(tmppty);
    tmpptx = this->endPointCapture.x()+this->borderEdgeWidth;
    tmppty = this->startPointCapture.y()+this->captureBorderWidth+this->borderEdgeWidth;
    endTmp.setX(tmpptx);
    endTmp.setY(tmppty);
    QRect rightTopRect(startTmp,endTmp);
    if(rightTopRect.width()>1 && rightTopRect.height()>1 &&
            rightTopRect.contains(curpos)){
        this->operateMode = CornerScale;
        this->cornerSide = RightTop;
        this->setCursor(Qt::SizeBDiagCursor);

        //qDebug()<<"judgeUponCorner RightTop"<<endl;
        return true;
    }

    //左下角
    tmpptx = this->startPointCapture.x()-this->borderEdgeWidth;
    tmppty = this->endPointCapture.y()-this->captureBorderWidth-this->borderEdgeWidth;
    startTmp.setX(tmpptx);
    startTmp.setY(tmppty);
    tmpptx = this->startPointCapture.x()+this->captureBorderWidth+this->borderEdgeWidth;
    tmppty = this->endPointCapture.y()+this->borderEdgeWidth;
    endTmp.setX(tmpptx);
    endTmp.setY(tmppty);
    QRect leftBottomRect(startTmp,endTmp);
    if(leftBottomRect.width()>1 && leftBottomRect.height()>1 &&
            leftBottomRect.contains(curpos)){
        this->operateMode = CornerScale;
        this->cornerSide = LeftBottom;
        this->setCursor(Qt::SizeBDiagCursor);
        //qDebug()<<"judgeUponCorner LeftBottom"<<endl;
        return true;
    }

    //右下角
    tmpptx = this->endPointCapture.x()-this->captureBorderWidth-this->borderEdgeWidth;
    tmppty = this->endPointCapture.y()-this->captureBorderWidth-this->borderEdgeWidth;
    startTmp.setX(tmpptx);
    startTmp.setY(tmppty);
    tmpptx = this->endPointCapture.x()+this->borderEdgeWidth;
    tmppty = this->endPointCapture.y()+this->borderEdgeWidth;
    endTmp.setX(tmpptx);
    endTmp.setY(tmppty);
    QRect rightBottomRect(startTmp,endTmp);
    if(rightBottomRect.width()>1 && rightBottomRect.height()>1 &&
            rightBottomRect.contains(curpos)){
        this->operateMode = CornerScale;
        this->cornerSide = RightBottom;
        this->setCursor(Qt::SizeFDiagCursor);
        //qDebug()<<"judgeUponCorner RightBottom"<<endl;
        return true;
    }

    return false;
}

//鼠标是否移动到边框
bool MaskFrameBase::judgeUponBorder(QPoint curpos)
{
    if(this->bLeftPressed){
        return false;
    }
    int border = dpiWidth(2);
    int starx = this->startPointCapture.x()+captureBorderWidth;
    int stary = this->startPointCapture.y()+captureBorderWidth;
    int endx = this->endPointCapture.x()-captureBorderWidth;
    int endy = this->endPointCapture.y()-captureBorderWidth;
    int width = qAbs(this->endPointCapture.x()-this->startPointCapture.x())-_rect_border_size.width()*2+lineWidth;
    int height = qAbs(this->endPointCapture.y()-this->startPointCapture.y())-_rect_border_size.height()*2+lineWidth;

    //左侧边框
    int tmpptx = starx-_rect_border_size.width()*0.5;
    int tmppty = stary;
    QPoint startTmp(tmpptx,tmppty);
    tmpptx = tmpptx + _rect_border_size.width();
    tmppty = tmppty +height-lineWidth;
    QPoint endTmp(tmpptx,tmppty);
    QRect leftborder(startTmp,endTmp);
    if(leftborder.width()>border && leftborder.height()>border &&
            leftborder.contains(curpos)){
        this->operateMode = BorderScale;
        this->borderSide = LeftBorder;
        this->setCursor(Qt::SizeHorCursor);
        qDebug()<<"judgeUponBorder:LeftBorder"<<endl;
        return true;
    }

    //右侧边框
    tmpptx = endx-_rect_border_size.width()*0.5;
    tmppty = stary;
    startTmp.setX(tmpptx);
    startTmp.setY(tmppty);
    tmpptx = tmpptx+_rect_border_size.width();
    tmppty = tmppty + height-lineWidth;
    endTmp.setX(tmpptx);
    endTmp.setY(tmppty);
    QRect rightborder(startTmp,endTmp);
    if(rightborder.width()>border && rightborder.height()>border &&
            rightborder.contains(curpos)){
        this->operateMode = BorderScale;
        this->borderSide = RightBorder;
        this->setCursor(Qt::SizeHorCursor);
        qDebug()<<"judgeUponBorder:RightBorder"<<endl;
        return true;
    }

    //上边框
    tmpptx = starx;
    tmppty = stary-_rect_border_size.width();
    startTmp.setX(tmpptx);
    startTmp.setY(tmppty);
    tmpptx = tmpptx+width-lineWidth;
    tmppty = tmppty+_rect_border_size.height();
    endTmp.setX(tmpptx);
    endTmp.setY(tmppty);
    QRect topborder(startTmp,endTmp);
    if(topborder.width()>1 && topborder.height()>1 &&
            topborder.contains(curpos)){
        this->operateMode = BorderScale;
        this->borderSide = TopBorder;
        this->setCursor(Qt::SizeVerCursor);
        qDebug()<<"judgeUponBorder:TopBorder"<<endl;
        return true;
    }

    //下边框
    tmpptx = starx;
    tmppty = endy-_rect_border_size.width()*0.5;
    startTmp.setX(tmpptx);
    startTmp.setY(tmppty);
    tmpptx = tmpptx+width-lineWidth;
    tmppty = tmppty+_rect_border_size.height();
    endTmp.setX(tmpptx);
    endTmp.setY(tmppty);
    QRect bottomborder(startTmp,endTmp);
    if(bottomborder.width()>border && bottomborder.height()>border &&
            bottomborder.contains(curpos)){
        this->operateMode = BorderScale;
        this->borderSide = BottomBorder;
        this->setCursor(Qt::SizeVerCursor);
        qDebug()<<"judgeUponBorder:BottomBorder"<<endl;
        return true;
    }

    return false;
}

//鼠标是否移动到弹出的工具条
bool MaskFrameBase::judgeUponBtnFrame(QPoint curpos)
{
    if(this->bLeftPressed){
        return false;
    }

    if(!this->btnFrame->isVisible()){
        //qDebug()<<"judgeUponBtnFrame not visible"<<endl;
        return false;
    }

    QPoint startpos = this->btnFrame->pos();
    QPoint endpos(startpos.x()+this->btnFrameWidth,startpos.y()+this->btnFrameHeigh);
    QRect btnRect(startpos,endpos);
    if(btnRect.width()>1 && btnRect.height()>1 &&
            btnRect.contains(curpos)){
        //qDebug()<<"judgeUponBtnFrame Normal"<<endl;
        this->operateMode = Normal;
        this->setCursor(Qt::ArrowCursor);
        return true;
    }

    return false;
}

//截屏区域是否有效(有效是指有有效面积)
bool MaskFrameBase::captureAreaValide()
{
    int width = this->endPointCapture.x()-this->startPointCapture.x();
    int heigh = this->endPointCapture.y()-this->startPointCapture.y();

    if(width < this->captureBorderWidth*2 ||
            heigh < this->captureBorderWidth*2){
        return false;
    }

    return true;
}

//判断点是否已在全屏的边界
bool MaskFrameBase::isPointToBorder(const QPoint& pt)
{
    if(pt.x()<=0 || pt.x()>=this->width() ||
            pt.y()<=0 || pt.y()>=this->height()){
        return true;
    }
    return false;
}

 

#ifndef MASK_FRAME_BASE_H
#define MASK_FRAME_BASE_H

#include <QPoint>
#include <QObject>
#include <QFrame>
#include "snappopupview.h"

class QLabel;
class QToolButton;

/**
 * @brief The MaskFrameBase class 抓屏/屏幕分享过程中的遮罩层-基类
 */
class MaskFrameBase : public QFrame {
    Q_OBJECT

protected:
    enum eOperateMode{
        Normal,//鼠标变成普通的指针形状
        SelectArea,//选取/重新选取区域
        MoveArea,//移动选定的区域
        BorderScale,//拖动边框进行缩放
        CornerScale//拖动边角进行缩放
    };

    enum eBorderSide{
        LeftBorder,
        TopBorder,
        RightBorder,
        BottomBorder
    };

    enum eCornerSide{
        LeftTop,
        LeftBottom,
        RightTop,
        RightBottom
    };

public:
    explicit MaskFrameBase(QWidget* parent = 0);
    virtual ~MaskFrameBase();

    void setup();
    void showMask();
    void reset();

protected:
    virtual void resizeEvent(QResizeEvent* event) override;
    virtual void mousePressEvent(QMouseEvent* event) override;
    virtual void mouseMoveEvent(QMouseEvent *event) override;
    virtual void mouseReleaseEvent(QMouseEvent *event) override;
    virtual void enterEvent(QEvent*) override;
    virtual void leaveEvent(QEvent*) override;
    virtual bool eventFilter(QObject *obj, QEvent *event) override;
    virtual void paintEvent(QPaintEvent *) override;

protected:
    virtual void createBtnFrame();
    virtual void showBtnFrame();

    virtual bool judgeOperateState(QPoint curpos);//鼠标未按下左键,移动过程中使用,会改变鼠标指针样式
    virtual bool updateCaptureRect();//更新截屏区域的坐标,更新了返回true
    virtual void adjustCaptureArea();//调整绘制区

    virtual void cancelSelect();
    void calRect();

protected:
    void setFixedParas();

    QPushButton* createActionButton(const QString &name, const QString& toolTipKey, const QString &icon);

    void alignPoint(QPoint& startpt,QPoint& endpt);//将一对点,整理一下,startpt返回左上
    void generateResultPos(QPoint startpt,QPoint endpt);
    bool captureAreaValide();//截屏区域是否有效(有效是指有有效面积)
    bool isPointToBorder(const QPoint& pt);//判断点是否已在全屏的边界

    bool judgeUponCorner(QPoint curpos);//鼠标是否移动到四个边角
    bool judgeUponBorder(QPoint curpos);//鼠标是否移动到边框
    bool judgeUponBtnFrame(QPoint curpos);//鼠标是否移动到弹出的工具条

    void updateCaptureRectMove();//移动时,改变抓屏区坐标

    void restrictMouseMove(QPoint& pt);

protected:
    QFrame* captureArea;//透明区域,选取图像
    QFrame* btnFrame;//操作按钮

    bool bSetupView;

    bool bLeftPressed;
    QPoint pressPoint;//鼠标点坐标
    QPoint curmousePoint;

    QPoint startPointCapture;//截屏区域
    QPoint endPointCapture;

    QPoint startPointCaptureMark;//按下鼠标瞬间的截屏区域
    QPoint endPointCaptureMark;

    eOperateMode operateMode;//鼠标移动时,操作的类型
    eBorderSide borderSide;//BorderScale模式下选定的是哪个边
    eCornerSide cornerSide;//CornerScale模式下选定的是哪个角

    int mScale;
    int btnHeigh;
    int btnFrameWidth;
    int btnFrameHeigh;
    int sizeTipFrameWidth;
    int sizeTipFrameHeigh;
    QString maskColor;
    QString capturBorderStyle;
    int captureBorderWidth;//边框
    int borderEdgeWidth;//判断鼠标移动到边框上时,边框区域=边框本身的宽度+borderEdgeWidth
    SnapPopupView * snapPopupView;

    //矩形
    QRect leftTopRect;
    QRect topRect;
    QRect rigtTopRect;
    QRect rightRect;
    QRect rightBottomRect;
    QRect bottomRect;
    QRect leftBottomRect;
    QRect leftRect;
    QRect centerRect;
    int  edgeMargin;     //鼠标检测的边缘距离
    QSize _rect_border_size;
    int lineWidth;
};

#endif // MASK_FRAME_BASE_H

 

Qt+OpenCV缩放图像至格子级别以及跟踪鼠标位置 最近需要做一个将图像放大至格子级别以及随着鼠标移动(不需要按下鼠标),实时显示鼠标位置以及对应的RGB的值。因此结合了opencv以及Qt做了一个这样的东西;大致效果如图; 实现的主要的主要思路是,通过OpenCV进行图像的处理和放大,然后转换为QImage在Qt中显示出来。        opencv缩放图像 首先是利用opencv进行图像的放大,利用的函数是resize 阅读详情

相关推荐

关于Qt中,设置视图层鼠标框选

为在Qt中设置鼠标框选

qq_45839137的博客 1076

Qt实现截图之二 截图裁剪框实现方案1 窗口组合方式

截图相关介绍参考Qt实现截图之一 整个截图分为两层,一层为截图显示区,一层为裁剪选着层,裁剪选择层蒙在截图显示区之上。 本节介绍一种方案实现裁剪框。 裁剪层分为5个区域,上下左右和中间,分别代表5个QWidget, 中间区域即为需要裁剪的区域-高亮区域,没有截图时此区域不存在。 1.布局 top, left,right, bottom 是个窗体均设置半透明颜色,mid是全透明效果就是裁剪区域 void MaskFrameBase::setup() { this->set.

xpg4321的博客 1596

Qt 简单截图工具(一) 高仿QQ截屏 滑动截屏

闲来无聊,在学习qt几个月后突然想制作一个截屏工具来给自己使用。简单分析了一下,认为截屏方式共可以分为三种: 获取当前窗口或控件的界面保存为图片 滑动鼠标截取当前桌面显示内容 识别鼠标指向窗口。控件或图标保存为图片

qq_43793182的博客 5168

QT-图片缩放的同时随意框选区域并保存截图

这篇文章里主要新写了一个myGraphicRectItem类,还是继承自QGraphicsItem类,用来实现: 1.在图像显示区域绘制矩形框 2.鼠标点击矩形的任意一条边可进行矩形的拉伸,同时鼠标样式更改 3.鼠标点击矩形右下角并拖动可进行矩形的缩放 4.鼠标按住矩形内区域并移动可拖动矩形 在这里插入图片描述 myGraphicRectItem.h代码如下: #ifndef MYGRAPHICRECTITEM_H #define MYGRAPHICRECTITEM_H #include &l...

机器视觉 2029

Qt 之 Camera 相机拍照并截图实现

背景 项目中,用到了拍照并截图,上传图片去后端拿题库答案,刚开始的实现是从屏幕中截图,然后保存成图片再上传,后来发现错了,因为是相当于屏幕截图,而实际上应该是拿相机拍照的图片做裁剪,来做后端的图片。项目中用到的是qml来实现。 类说明 window下相关类及使用 QCamera类 QCamera类是直接对应摄像头的类,包括如下方法: 1、QCamera() 创建摄像头类对象实例。 2、setViewfinder(viewfinder) 设置取景器,取景器就是将图像实时在屏幕显示,就跟相机的屏幕一样,参数是取

一个老菜鸟的学习分享 5077

Qt通用截图截屏实现

在Qt中,实现通用的截图截屏功能是一项十分常见的需求。本文将介绍如何使用Qt实现通用的截图截屏,并提供相应的源代码。使用以上方法即可轻松实现通用的截图截屏,且能够应对多种不同的场景需求。其中,x、y、w、h为所需要的区域的坐标和大小。Qt通用截图截屏实现。

DevAstro的博客 1209

Qt中截图功能的实现

提要需求:加载一张图片并显示,可以放大缩小,可以截取图片的某个矩形并保存。原以为蛮简单的一个功能,其实还是有点小复杂。最简单Qt图片浏览器可以参考Qt自带的Demo:Image Viewer Example - http://qt-project.org/doc/qt-4.8/widgets-imageviewer.html看一下最终的实现效果:图片的加载显示这里需要实现一个QImageViewe

爱冒险的技术宅 1万+

使用Qt实现屏幕截图功能教程

在现代计算机应用程序开发中,Qt是一个跨平台的C++框架,广泛应用于开发GUI程序。它由挪威Trolltech公司开发,并自2008年起由Digia公司维护,随后在2014年被Qt Company正式接管。Qt的突出特点包括一个直观且强大的信号与槽机制用于对象间通信,以及丰富的API,这些API为桌面、嵌入式和移动平台提供了深度支持。Qt不仅提供了丰富的窗口控件和绘图功能,还支持模块化和插件架构,允许开发者根据需要选择性地引入功能。

weixin_35414260的博客 906

Qt5实现视频截图

功能: 在使用 qt 实现的播放器中,增加截图功能 实现思路: 使用qt的全屏截图方法,获取整个屏幕的截图 获取当前程序的坐标,裁剪整个屏幕的截图,获取所需的图片区域 代码实现: # 截图 def castVideo(self) # 截取当前整个屏幕 screen = QGuiApplication.primaryScreen() cast_jpg = f'{os.path.dirname(os.path.dirname(os.path.dirname(_

qq_17328759的博客 6922

Qt中在QLabel上载入图片后在上面画矩形框,并将矩形框选的区域显示

实在受不了了,网上搜的那些都只有框选,并没有把框选的区域显示,只能自己干了,希望能够帮到你们

Qt5实现的图片ROI区域的框选、移动、缩放

基于VS+Qt5实现的用于图片ROI区域框选,并可以对区域进行移动缩放删除保存操作。主要通过重写QLabel,代码仅供参考。 介绍链接:https://blog.youkuaiyun.com/qq_36131739/article/details/104855098

QT框架实现自定义形状截图效果

截图实际上是先将整个屏幕画面保存到像素图中,然后将截图窗口调整到跟屏幕同样大小,并将像素图绘制到截图窗口中。所以在截图状态下用户看到的屏幕画面是截图窗口显示的图片而已,并不是原始的屏幕画面。在用户按下鼠标左键时启动拉框操作,鼠标移动过程中改变橡皮框大小,鼠标左键释放时完成拉框操作并以橡皮框区域作为截图区域。比如F1的虚拟键盘码可以用VK_F1表示,字母X的键盘码可以用’X’表示。实际上仍然是采用多边形裁剪区域进行图像截取,只是让用户通过拖动鼠标来选择多边形的顶点,所以效果上看就是任意形状的截图区域。

QT性能优化QT原理源码QT界面美化 723

图片上ROI区域矩形框的选取

主要通过重写qlabel显示图像,并在图像上画出矩形框进行图片ROI区域的选取,主要用到了重新定义类、鼠标事件、图片显示函数。

QT PC端实现图片裁剪选择框

文章目录需求需求分析开发实现 需求 实际项目中经常需要选择本地图片的部分预览、剪切、上传处理后图片到服务端,上图,效果图: 这个是最近QT实现的PC项目里的需求和成品截图。这里讲讲上图红色圈圈里的选择框实现。 需求分析 UI实现:外侧需要半透明蒙层,虚线的选择框和四个缩放用的顶点,内侧透明镂空交互:四个点可以缩放,镂空处可以移动,且边框边界限制 开发实现 实现比较简单,直接上代码,具体解释在代码里都有了 ui文件: <?xml version="1.0" encoding="UTF-8"?>

weixin_41605683的博客 2741

Qt 之 简单截图功能(三)实现可拖拽选中区域

一个简单的小例子来看一看如何用Qt实现类似QQ的截图功能,详情见博客 :http://blog.youkuaiyun.com/GoForwardToStep/article/details/54385140

达梦7实时主备集群

达梦7实时主备配置实时主备数据环境说明配置主库配置 dm.ini配置dmmal.ini配置dmarch.ini配置dmwatcher.ini配置dmwatcher.ini以mount方式启动主库设置OGUID修改数据库为primary模式配置备库 在服务器部署过程中,通常搭建有数据库主备集群,用于防备数据库服务器产生问题影响数据正常,在此,我们分享一下在虚拟机环境中模拟搭建实时主备的数据守护模式。 在搭建环境之前,需用户确认主备DM7的数据库已安装完成,且版本一致,同时还应注意各实例所在主机的操作系统位数、

g360382的博客 1022

qt截图代码怎么在linux里面使用方法,用QT实现类似QQ截图的工具

widget.cpp#include "widget.h"#include "ui_widget.h"#include Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget){ui->setupUi(this);createWidgets();createConnects();createEventFilter(...

weixin_39625098的博客 576

海康威视 2DC 系列球机手机直连实用教程

海康威视2DC系列球机手机直连教程:首先确认设备支持直连功能并下载官方"海康互联"App;通过网线连接球机与路由器,使用SADP软件调整IP至同一网段;在App中添加设备并激活,完成调试即可远程查看监控画面。常见问题包括部分型号需远程解锁、网络连接异常等,可通过检查网络环境、联系客服等方式解决。本教程提供从准备到连接的完整指引,助您轻松实现手机监控管理。

海康威视技术支持 5412

基于Qt OpenGL的图形绘制系统的设计与开发

总体来看,需要实现的系统的主要功能为通过交互,绘制图形,并保存为.bmp 格式的文件。(3) 系统包含的绘图功能:基本图形的绘制(直线、圆、椭圆、矩形、平行四边形、圆角矩形等)、多边形的绘制、曲线的绘制(Bezier曲线、二次B样条曲线等)、图形的裁剪、图形属性(线宽、线型)的设置、图形的擦除、图形的保存等功能。用户点击画板上两点绘制圆角矩形,第一点为对角线起点,第二点为对角线终点。用户点击画板上三点绘制椭圆,第一点为椭圆圆心,第二点为长轴上的一个端点,第三点为短轴上的一个端点。(8)Bezier曲线。

毕业作品网站 1992

如何用Hugo Stack主题打造极简技术博客?3个必改配置与5个高级美化技巧

本文详细解析了如何深度定制Hugo Stack主题,打造个性化技术博客。文章提供了3个必改配置与5个高级美化技巧,涵盖字体美学、布局重构、代码块视觉强化及细节打磨,帮助开发者超越基础部署,实现从功能到美学的进阶,提升博客质感与用户体验。

carrot的博客 569

读懂 403 Forbidden, the request has been blocked by UCON.:它不是浏览器渲染问题,而是后端安全闸门在拦截

摘要 403 Forbidden 错误表明服务器理解请求但拒绝执行,通常与权限或访问控制有关。当错误信息显示 "blocked by UCON" 时,说明请求被 SAP 体系中的统一连接管控框架拦截。UCON 采用白名单机制限制外部调用,通过记录、评估、强制三阶段流程实施管控。常见触发场景包括 HTTP 服务与通信场景配置不一致、白名单未更新等。排查时应通过 DevTools 确认服务器响应,使用 curl 排除浏览器干扰,并比较不同环境的行为差异。解决方案通常涉及调整配置传输顺序或更新

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 241
上一篇: Qt实现截图之二 截图裁剪框实现方案1 窗口组合方式
下一篇: 菜鸟学ffmpeg音视频技术之6 mac截屏
很菜但努力
博客等级 码龄17年 113粉丝 50原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值