dhgftchfhg 2024-01-18 11:22 采纳率: 20%
浏览 5

请问一下各位大老,在qt中opencv的问题。

请问一下,本意是让imageLabel 显示图片,图片中有白框可以拖动。但是在mouseDoubleClickEvent中点击了modifyBtn按钮,后,调用imageLabel 的setpixmap显示,这个时候显示的这个图片是没有白框的,paintevent重绘的是imageLabel 这个类,那么imageLabel 的setpixmap显示的图片怎么没啥作用啊,请问一下各位。


#include "labdialog.h"
#include "qdebug.h"
#include "qpainter.h"
#include "qpushbutton.h"
#include <QDialog>
#include <QHBoxLayout>
#include <QLabel>

Labeldialog::Labeldialog(QWidget *parent)
    : QLabel(parent)
{
//    setPixmap(m_image);

}

void Labeldialog::SetImage(QImage image)
{
    m_image =image;
    setPixmap(QPixmap::fromImage(m_image.scaled(100,100)));
}

void Labeldialog::SetOldImagePath(QString & path)
{
    oldimgPath = path;
}

void Labeldialog::ResultImagePath(QString & path)
{
    resultimg_Path = path;
}

void Labeldialog::setRect(QRect& rect)
{
    image_rect = rect;
}

void Labeldialog::mouseDoubleClickEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        QDialog* dialog = new QDialog(this);

        imageLabel = new QLabel();
        imageLabel->setMinimumSize(600,600);
        imageLabel->setPixmap(QPixmap::fromImage(m_image));

        QHBoxLayout* hb = new QHBoxLayout();
        QHBoxLayout* allhb = new QHBoxLayout();
        QVBoxLayout* vb = new QVBoxLayout();
        QPushButton* modifyBtn = new QPushButton(QString::fromLocal8Bit("修改"));
        QPushButton* enter = new QPushButton(QString::fromLocal8Bit("确认"));
        enter->setVisible(false);

        connect(modifyBtn,&QPushButton::clicked,this,[enter, this](){
            //显示确认按钮
            enter->setVisible(true);
            mMat = cv::imread("C:/cv/tttt.jpeg");
            cv::cvtColor(mMat, mMat, cv::COLOR_BGR2RGB);
            m_pt = QRect(100,100,414,388);//image_rect;

            int nThisWidth = this->width();
            int nThisHeight = this->height();

            //坐标转换
            showrect.setX(this->width() * m_pt.x() / mMat.cols);
            showrect.setY(this->height() * m_pt.y() / mMat.rows);
            showrect.setWidth(this->width() * m_pt.width() / mMat.cols);
            showrect.setHeight(this->height() * m_pt.height() / mMat.rows);
            // 转换 OpenCV 的图像数据为 Qt 图像

            QImage img = QImage(mMat.data, mMat.cols, mMat.rows, mMat.step, QImage::Format_RGB888);
            // 将图像显示在 QLabel 上
            imageLabel->setPixmap(QPixmap::fromImage(img));

            setMouseTracking(true);

            imageLabel->repaint();
        });

        hb->addWidget(imageLabel);
        vb->addWidget(modifyBtn);
        vb->addWidget(enter);
        allhb->addLayout(hb);
        allhb->addLayout(vb);
        dialog->setLayout(allhb);
        dialog->setWindowTitle("ImageConfig");
        dialog->show();
    }
    else
    {
        QLabel::mousePressEvent(event);
    }
}

void Labeldialog::mousePressEvent(QMouseEvent* event)
{
    if (m_isleft)
    {
        grabMouse();
        m_isclieckdown = true;
    }
}

void Labeldialog::mouseMoveEvent(QMouseEvent* event)
{
    if (!m_isclieckdown)
    {
        if (event->x() > showrect.x() - 10 && event->x() < showrect.x() + 10)
        {
            this->setCursor(Qt::SizeHorCursor);
            m_isleft = true;
        }
        else
        {
            this->setCursor(Qt::ArrowCursor);
        }
    }
    else
    {
        showrect.setX(event->x());
        update();
    }
}

void Labeldialog::mouseReleaseEvent(QMouseEvent* event)
{
    releaseMouse();
    m_isleft = false;
    m_isclieckdown = false;
}

void Labeldialog::paintEvent(QPaintEvent* event)
{
    QPainter painter(this);
    painter.save();

    QRect widgRect = this->rect();
    QImage img = QImage((uchar*)mMat.data, mMat.cols, mMat.rows, mMat.cols * mMat.channels(), QImage::Format_RGB888);
    painter.drawImage(widgRect, img);

    painter.setPen(QColor::fromRgb(255, 255, 255));
    painter.drawRect(showrect);

    painter.restore();
}
  • 写回答

2条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2024-01-18 14:25
    关注

    【以下回答由 GPT 生成】

    问题的根本原因是在mouseDoubleClickEvent中调用了imageLabel的setPixmap方法,但是没有重新绘制imageLabel,导致白色框没有显示。

    解决方案如下:

    1. 首先,在Labeldialog类中添加一个成员变量dragRect用于保存拖动框的位置和尺寸。
    private:
        QRect dragRect; // 拖动框的位置和尺寸
    
    1. 然后,在Labeldialog类的paintEvent中绘制白色拖动框。
    void Labeldialog::paintEvent(QPaintEvent *event)
    {
        QLabel::paintEvent(event);
    
        QPainter painter(this);
    
        painter.setPen(Qt::white);
        painter.drawRect(dragRect);
    }
    
    1. 修改setRect方法,在其中更新拖动框的位置和尺寸,并调用update方法重新绘制。
    void Labeldialog::setRect(QRect& rect)
    {
        dragRect = rect;
        update();
    }
    
    1. 修改mouseDoubleClickEvent方法,在其中添加重新绘制拖动框的代码。
    void Labeldialog::mouseDoubleClickEvent(QMouseEvent *event)
    {
        if(event->button() == Qt::LeftButton)
        {
            QDialog* dialog = new QDialog(this);
    
            imageLabel = new QLabel();
            imageLabel->setMinimumSize(600,600);
            imageLabel->setPixmap(QPixmap::fromImage(m_image));
    
            QHBoxLayout* hb = new QHBoxLayout();
            QHBoxLayout* allhb = new QHBoxLayout();
            QVBoxLayout* vb = new QVBoxLayout();
            QPushButton* modifyBtn = new QPushButton(QString::fromLocal8Bit("修改"));
            QPushButton* enter = new QPushButton(QString::fromLocal8Bit("确认"));
            enter->setVisible(false);
    
            connect(modifyBtn, &QPushButton::clicked, this, &Labeldialog::modifyImage);
    
            // 新增的代码,将图片框添加到对话框中
            hb->addWidget(imageLabel);
            vb->addWidget(modifyBtn);
            vb->addWidget(enter);
            allhb->addLayout(hb);
            allhb->addLayout(vb);
            dialog->setLayout(allhb);
    
            // 添加新的代码,设置拖动框的初始位置和尺寸
            setRect(QRect(100, 100, 200, 200));
    
            dialog->exec();
        }
    }
    

    以上就是解决该问题的具体步骤和代码修改,希望可以帮到你。如果还有其他问题,请随时提出。


    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

问题事件

  • 创建了问题 1月18日

悬赏问题

  • ¥15 SQL Server下载
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求