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);
    
    
    }
  • 相关阅读:
    MySQL limit 分页查询优化(百万级优化)
    HAVING 搜索条件在进行分组操作之后应用
    Mysql 多表连接查询 inner join 和 outer join 的使用
    php7 configure: error: Cannot find OpenSSL's <evp.h> 问题解决
    解决Cannot find config.m4 Make sure that you run '/home/php/bin/phpize' in the top level source directory of the module
    Ubuntu下彻底卸载默认安装的mysql,自己手动下载安装MYSQL
    mysql 运算操作符
    mysql 函数表
    【mysql】连接和断开服务器
    mysql 5.7 增删改查及别名的用法
  • 原文地址:https://www.cnblogs.com/ike_li/p/12899033.html
Copyright © 2011-2022 走看看