QLabel显示图片和框选ROI

一、

二、

 

 

#ifndef DISPLAYIMAGELABEL_H
#define DISPLAYIMAGELABEL_H

#include <QObject>
#include <QLabel>
#include <QPixmap>
class DisplayImageLabel : public QLabel
{
    Q_OBJECT
public:
    explicit DisplayImageLabel(QWidget *parent = nullptr);
public:
    void setSize(int w,int h);
    void setPixmap(QPixmap pixmap);
    void updateTargetRect(QRect rect);
    void showText(QString txt,QColor txtColor=QColor(Qt::blue),QPoint loc=QPoint(10,20));
signals:
    void sigSelectedRect(QRect rect);
    void sigStartSelect();
public slots:
protected:
    void mousePressEvent(QMouseEvent *ev) override;
    void mouseMoveEvent(QMouseEvent *ev) override;
    void mouseReleaseEvent(QMouseEvent *ev) override;
    void paintEvent(QPaintEvent *) override;
private:
    bool   isStartSelect;
    QPoint startPoint;
    QPoint endPoint;
    QPoint textLoc;
    QString text;
    QColor textColor;
    QPixmap pixImage;


};

#endif // DISPLAYIMAGELABEL_H

 

#include "displayimagelabel.h"
#include <QMouseEvent>
#include <QPen>
#include <QPainter>
#include <QDebug>
DisplayImageLabel::DisplayImageLabel(QWidget *parent)
    : QLabel(parent)
    ,isStartSelect(false)
    ,startPoint(0,0)
    ,endPoint(0,0)
    ,textLoc(10,20)
    ,textColor(QColor(Qt::blue))

{
    this->setStyleSheet("background-color:#A9A9A9");
}

void DisplayImageLabel::setSize(int w, int h)
{
    this->setMinimumSize(w,h);
    this->setMaximumSize(w,h);
}

void DisplayImageLabel::setPixmap(QPixmap pixmap)
{
    pixImage=pixmap;
}

void DisplayImageLabel::updateTargetRect(QRect rect)
{
    startPoint=QPoint(rect.x(),rect.y());
    endPoint=QPoint(rect.x()+rect.width(),rect.y()+rect.height());
    update();
}

void DisplayImageLabel::showText(QString txt, QColor txtColor, QPoint loc)
{
    textLoc=loc;
    text=txt;
    textColor=txtColor;
}

void DisplayImageLabel::mousePressEvent(QMouseEvent *ev)
{
    // 如果是鼠标左键按下
    if(ev->button() == Qt::LeftButton)
    {
       isStartSelect=true;
       startPoint=ev->pos();
       emit sigStartSelect();
    }

}

void DisplayImageLabel::mouseMoveEvent(QMouseEvent *ev)
{


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


}

void DisplayImageLabel::mouseReleaseEvent(QMouseEvent *ev)
{

    if(isStartSelect)
    {
        isStartSelect=false;
        endPoint=ev->pos();
        emit sigSelectedRect(QRect(startPoint.x(),startPoint.y(),endPoint.x()-startPoint.x(),endPoint.y()-startPoint.y()));
    }
}

void DisplayImageLabel::paintEvent(QPaintEvent *)
{

    QPainter painter(this);
    painter.setRenderHint(QPainter::TextAntialiasing, true);
    painter.setRenderHint(QPainter::SmoothPixmapTransform, true);

    painter.drawPixmap(0, 0, this->width(), this->height(), pixImage);

    painter.setPen(QPen(Qt::blue,2));
    painter.drawRect(startPoint.x(),startPoint.y(),endPoint.x()-startPoint.x(),endPoint.y()-startPoint.y());

    painter.setPen(QPen(textColor,1));
    painter.drawText(textLoc,text);


}

 

posted @ 2020-05-16 10:31  ike_li  阅读(1192)  评论(1编辑  收藏  举报