Qt C++ 图像显示组件 paintEvent 事件优化
void CImageShowWidget::paintEvent(QPaintEvent* event) { QWidget::paintEvent(event); if (m_pixmap.isNull()) { ui->ImageLab->clear(); return; }
QPixmap pix = m_pixmap.scaled(ui->ImageLab->size());
QPainter pLine(ui->ImageLab);
pLine.drawPixmap(0, 0, pix);
pLine.setRenderHint(QPainter::Antialiasing);
pLine.setRenderHint(QPainter::SmoothPixmapTransform);
// 绘制通道id到图上
DrawChannelNum(pLine, m_channelID);
// 绘制ROI矩形
if (m_RoiRect.width() > 0 && m_RoiRect.height() > 0)
{
if (m_bChangeROIColor)
{
pLine.setPen(Qt::green);
float fWScale = (float)m_pixImage.width() / (float)ui->ImageLab->width();
float fHScale = (float)m_pixImage.height() / (float)ui->ImageLab->height();
m_fRectROIScale = fWScale > fHScale ? fHScale : fWScale;
}
else
{
pLine.setPen(Qt::red);
}
pLine.setBrush(Qt::NoBrush);
QRect rectRoi = m_RoiRect;
rectRoi.setX(m_RoiRect.x() - ui->ImageLab->x());
rectRoi.setY(m_RoiRect.y() - ui->ImageLab->y());
rectRoi.setWidth(m_RoiRect.width());
rectRoi.setHeight(m_RoiRect.height());
pLine.drawRect(rectRoi);
}
// 绘制角度
if (m_degDrawingPoint.size() > 1 || m_degPoint.size() > 0)
{
pLine.setPen(Qt::red);
pLine.setBrush(Qt::NoBrush);
if (m_degDrawingPoint.size() > 1)
{
if (m_degDrawingPoint.size() == 2)
{
int iX = ui->ImageLab->x();
int iY = ui->ImageLab->y();
pLine.drawLine(m_degDrawingPoint[0].x() - ui->ImageLab->x(), m_degDrawingPoint[0].y() - ui->ImageLab->y(), m_degDrawingPoint[1].x() - ui->ImageLab->x(), m_degDrawingPoint[1].y() - ui->ImageLab->y());
}
else if (m_degDrawingPoint.size() == 3)
{
pLine.drawLine(m_degDrawingPoint[0].x() - ui->ImageLab->x(), m_degDrawingPoint[0].y() - ui->ImageLab->y(), m_degDrawingPoint[1].x() - ui->ImageLab->x(), m_degDrawingPoint[1].y() - ui->ImageLab->y());
pLine.drawLine(m_degDrawingPoint[0].x() - ui->ImageLab->x(), m_degDrawingPoint[0].y() - ui->ImageLab->y(), m_degDrawingPoint[2].x() - ui->ImageLab->x(), m_degDrawingPoint[2].y() - ui->ImageLab->y());
QVector<QPoint> posResult = GetArcPoint(m_degDrawingPoint);
if (posResult.size() == 3)
{
ArcInfo arcinfo = DrawArc(posResult[1] - ui->ImageLab->pos(), posResult[2] - ui->ImageLab->pos(), posResult[0] - ui->ImageLab->pos(), false);
pLine.drawArc(arcinfo.rectangle, arcinfo.startAngle * 16, arcinfo.spanAngle * 16);
pLine.drawText(m_degDrawingPoint[0] - ui->ImageLab->pos(), QString::number(arcinfo.spanAngle) + "°");
}
}
}
if (m_degPoint.size() > 0)
{
for (size_t i = 0; i < m_degPoint.size(); i++)
{
QVector<QPoint> posInt = m_degPoint[i];
if (posInt.size() == 3)
{
pLine.drawLine(posInt[0].x() - ui->ImageLab->x(), posInt[0].y() - ui->ImageLab->y(), posInt[1].x() - ui->ImageLab->x(), posInt[1].y() - ui->ImageLab->y());
pLine.drawLine(posInt[0].x() - ui->ImageLab->x(), posInt[0].y() - ui->ImageLab->y(), posInt[2].x() - ui->ImageLab->x(), posInt[2].y() - ui->ImageLab->y());
QVector<QPoint> posResult = GetArcPoint(posInt);
if (posResult.size() == 3)
{
ArcInfo arcinfo = DrawArc(posResult[1] - ui->ImageLab->pos(), posResult[2] - ui->ImageLab->pos(), posResult[0] - ui->ImageLab->pos(), false);
pLine.drawArc(arcinfo.rectangle, arcinfo.startAngle * 16, arcinfo.spanAngle * 16);
pLine.drawText(posInt[0] - ui->ImageLab->pos(), QString::number(arcinfo.spanAngle) + "°");
}
}
}
}
}
// 绘制Profile线段
if (m_DrawProfilePoint.size() == 2)
{
pLine.setPen(Qt::red);
pLine.setBrush(Qt::NoBrush);
if (m_DrawProfilePoint.size() == 2)
{
pLine.drawLine(m_DrawProfilePoint[0].x() - ui->ImageLab->x(), m_DrawProfilePoint[0].y() - ui->ImageLab->y(), m_DrawProfilePoint[1].x() - ui->ImageLab->x(), m_DrawProfilePoint[1].y() - ui->ImageLab->y());
}
}
// 绘制LineSegment
if (m_LineSegmentPoint.size() == 2)
{
pLine.setPen(Qt::red);
pLine.setBrush(Qt::NoBrush);
if (m_LineSegmentPoint.size() == 2)
{
pLine.drawLine(m_LineSegmentPoint[0].x() - ui->ImageLab->x(), m_LineSegmentPoint[0].y() - ui->ImageLab->y(), m_LineSegmentPoint[1].x() - ui->ImageLab->x(), m_LineSegmentPoint[1].y() - ui->ImageLab->y());
float iDiffX = abs(m_LineSegmentPoint[1].x() - m_LineSegmentPoint[0].x())*m_pixmap.width() / pix.width();
float iDiffY = abs(m_LineSegmentPoint[1].y() - m_LineSegmentPoint[0].y())*m_pixmap.height() / pix.height();
float fLength = sqrt(iDiffX*iDiffX + iDiffY * iDiffY);
if (fLength > 0)
{
if (m_PixelWidth > 0)
{
pLine.drawText(QPoint(m_LineSegmentPoint[1].x() - ui->ImageLab->x(), m_LineSegmentPoint[1].y() - ui->ImageLab->y()), QString::number(fLength * m_PixelWidth) + "μm");
}
else
{
pLine.drawText(QPoint(m_LineSegmentPoint[1].x() - ui->ImageLab->x(), m_LineSegmentPoint[1].y() - ui->ImageLab->y()), QString::number(fLength) + "pix");
}
}
//if (m_PixelWidth > 0)
// {
// float iDiffX = abs(m_LineSegmentPoint[1].x() - m_LineSegmentPoint[0].x())*m_pixmap.width() / pix.width();
// float iDiffY = abs(m_LineSegmentPoint[1].y() - m_LineSegmentPoint[0].y())*m_pixmap.height() / pix.height();
// float fLength = sqrt(iDiffX*iDiffX + iDiffY * iDiffY);
// if (fLength > 0)
// {
// pLine.drawText(QPoint(m_LineSegmentPoint[1].x() - ui->ImageLab->x(), m_LineSegmentPoint[1].y() - ui->ImageLab->y()), QString::number(fLength) + "μm");
// }
// }
}
}
if (m_bShowScale && m_struScaleStyleSheet.m_bShow)
{
pLine.setBrush(Qt::NoBrush);
DrawScale(pLine);
}
g_mutex.lock();
m_pix = pix;
g_mutex.unlock();
if (m_CropUpRect.isValid())
{
QRect rectRoi = m_CropUpRect;
rectRoi.setX(m_CropUpRect.x() - ui->ImageLab->x());
rectRoi.setY(m_CropUpRect.y() - ui->ImageLab->y());
rectRoi.setWidth(m_CropUpRect.width());
rectRoi.setHeight(m_CropUpRect.height());
//QPixmap pixCrop = pix.copy(rectRoi);
//emit Signal_setPixmap(pixCrop.scaled(ui->ImageLab->size()));
emit Signal_updateImage(rectRoi);
}
else
{
emit Signal_updateImage();
//emit Signal_setPixmap(pix);
}
}
原文地址: https://www.cveoy.top/t/topic/p941 著作权归作者所有。请勿转载和采集!