在图像QT周围绘制边框。

时间:2016-04-15 10:44:40

标签: c++ qt

我在使用此代码时遇到一些问题,在QT中围绕图像绘制边框,任何人都可以告诉我我缺少的内容:

void imageLabel::paintEvent(QPaintEvent *event)
{
QLabel::paintEvent(event);
if (!m_qImage.isNull())
{
    QImage qImageScaled =  m_qImage.scaled(QSize(width(),height()),Qt::KeepAspectRatio,Qt::FastTransformation);
double dAspectRatio = (double)qImageScaled.width()/(double)m_qImage.width();
int iX = m_iX*dAspectRatio;
int iY = m_iY*dAspectRatio;
int iWidth = m_iWidth*dAspectRatio;
int iHeight = m_iHeight*dAspectRatio;

QPainter qPainter(this);
qPainter.drawImage(0,0,qImageScaled);
qPainter.setBrush(Qt::NoBrush);
qPainter.setPen(Qt::red);
qPainter.drawRect(iX,iY,iWidth,iHeight);
}
}

1 个答案:

答案 0 :(得分:1)

You can use QFrame to simplify the task of adding a frame around a widget like QLabel.

In QtCreator, simply select the label and scroll down until you see the turquoise section of the properties editor and play with the values there.

QFrame settings in properties editor of QtCreator

The result looks like this:

A QLabel with QFrame

Hope this helps you along!

相关问题