• 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);
    
    
    }
  • 相关阅读:
    SpringBoot整合WebSocket的客户端和服务端的实现
    Django实现发送邮件
    Python环境搭建
    Hexo+Gitee搭建个人博客
    Chrome浏览器安装离线插件Markdown Here
    TestLink测试用例管理工具使用说明
    【odoo14】【好书学习】odoo 14 Development Cookbook【目录篇】
    【odoo14】【开发侧】权限配置
    【odoo14】【用户侧】权限配置
    【odoo14】【知识点】视图的继承逻辑
  • 原文地址:https://www.cnblogs.com/ike_li/p/12899033.html
Copyright © 2020-2023  润新知