zoukankan      html  css  js  c++  java
  • 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);
    
    
    }
  • 相关阅读:
    thinkpa R61i安装XP SATA的解决方法
    成都港宏4S店买的日产,送的无牌DVD 和可视倒车品牌是路特仕 80007
    设计模式之:解剖观察者模式
    java使用siger 获取服务器硬件信息(CPU 内存 网络 io等)
    lephone 壁纸(裸婚时代 童佳倩姚笛壁纸)
    C# 让程序自动以管理员身份运行
    项目管理的5大过程组、9大知识域、44个管理流程的映射关系
    Spring Security 中如何让用户名不存在的错误显示出来(用户名不存在显示Bad credentials)
    eclipse老是报ThreadPoolExecutor$Worker.run()
    Unison文件夹同步工具
  • 原文地址:https://www.cnblogs.com/ike_li/p/12899033.html
Copyright © 2011-2022 走看看