请问一下,本意是让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();
}