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);
    
    
    }
  • 相关阅读:
    Oracle 查询语句截取字符串
    删除ORACLE数据库中重复的数据
    [易学C#]C#3.0语言新特性之匿名类型
    [讨论]程序之路在何方?
    [易学C#]C#3.0语言新特性之扩展方法
    [易学C#]C#3.0语言新特性之对象和集合初始化器
    用C#解决Oracle9i和Oracle10g字符集不兼容的问题
    一个C#操作Oracle的通用类
    [易学C#]C#3.0语言新特性之隐式类型
    重拾 DirectX 一:配置Visual Studio 2008+Microsoft DirectX SDK (June 2008) 开发环境
  • 原文地址:https://www.cnblogs.com/ike_li/p/12899033.html
Copyright © 2011-2022 走看看