zoukankan      html  css  js  c++  java
  • Qt Creator中如何在QLabel上画矩形框并且保存画完后的图形

    问题描述:

    在QLabel上显示一张图片,然后在该图片上画一个矩形框  最后保存一张带矩形框的图片

    第一步:

    在图片上画矩形框   网上教程很多  代码如下: 头文件

    #ifndef MYQLABEL
    #define MYQLABEL
    
    #include <QLabel>
    #include <QMouseEvent>
    #include <QString>
    
    class MyQLabel : public QLabel{
    //    Q_OBJECT
    public:
    //    explicit MyQLabel();
        void paintEvent(QPaintEvent* event);
        void mousePressEvent(QMouseEvent* event);
        void mouseReleaseEvent(QMouseEvent* event);
        void mouseMoveEvent(QMouseEvent* event);
        int x1, y1, x2, y2;
    
        void savePicture(QString path, int filename);
    
    public:
        int getX1() {return x1;}
        int getY1() {return y1;}
        int getX2() {return x2;}
        int getY2() {return y2;}
    };
    
    #endif // MYQLABEL
    

    源文件:

    #include <QPainter>
    #include <QPen>
    #include <QRect>
    #include <QCursor>
    #include <QApplication>
    #include <QPixmap>
    #include <QImage>
    
    #include "myqlabel.h"
    
    void MyQLabel::paintEvent(QPaintEvent *event)
    {
        //comment before
        QLabel::paintEvent(event); //绘制背景的图片
    
        QPainter painter(this);
    
        painter.setPen(QPen(Qt::red, 2));
        painter.drawRect(QRect(x1, y1, x2 - x1, y2 - y1));
    }
    
    void MyQLabel::mousePressEvent(QMouseEvent *event)
    {
        x1 = event->pos().x();
        y1 = event->pos().y();
        QCursor cursor;
        cursor.setShape(Qt::ClosedHandCursor);
        QApplication::setOverrideCursor(cursor);
    }
    
    void MyQLabel::mouseReleaseEvent(QMouseEvent *event)
    {
        x2 = event->pos().x(); //鼠标相对于所在控件的位置
        y2 = event->pos().y();
        update();
        QApplication::restoreOverrideCursor();
    
    }
    
    void MyQLabel::mouseMoveEvent(QMouseEvent *event)
    {
        if (event->buttons() & Qt::LeftButton) {
            x2 = event->pos().x(); //鼠标相对于所在控件的位置
            y2 = event->pos().y();
            update();
        }
    }
    

    第二步:

    保存画完后的整个图形

    思路如下:

    取得QLabel上的图像  将其加入到一个Painter   然后获取之前画的矩形框的  起始 和终止坐标  重新绘制 然后保存

    QImage tmpimage = image.copy();
    
        QPixmap pmap;
        pmap = pmap.fromImage(tmpimage);
        label_video1->setPixmap(pmap);
    
        QImage img(label_video1->pixmap()->toImage());
        QPainter painter;
        painter.begin(&img);
        painter.setPen(QPen(Qt::red, 2));
        painter.drawRect(QRect(label_video1->getX1(), label_video1->getY1(),
                               label_video1->getX2() - label_video1->getX1(),
                               label_video1->getY2() - label_video1->getY1()));
        painter.end();
        label_video2->setPixmap(QPixmap::fromImage(img));
    
  • 相关阅读:
    转 oracle catalog 库常用脚本
    转 【ORACLE】ORA-12537 问题整理
    转 Trace a specific ORA- error
    15%
    MySQL 存储过程
    MySQL 命令行客户机的分隔符
    MySQL 连接join
    MySQL 正则表达式
    MySQL 日期时间函数
    Arthas 快速入门
  • 原文地址:https://www.cnblogs.com/lifeng-blog/p/9057509.html
Copyright © 2011-2022 走看看