如何使用QPainter绘制焦点图标

时间:2017-02-02 11:48:49

标签: c++ qt

在我正在工作的项目中,我们已经使用QPaint绘制了左箭头(可以看到左下角)代码如下:

const qreal x1 = left ? (rect.left() + 19) : (rect.right() - 19);
const qreal x2 = left ? (rect.left() + 12) : (rect.right() - 12);
const qreal y2 = rect.y() + rect.height() / 2.0;
const qreal y1 = y2 - 8;
const qreal y3 = y2 + 8;
painter->setPen(QPen(InternalStuff::Instance()->GetColor(ILMCTheme::ColorIconInfoBarArrow), 3, Qt::SolidLine, Qt::RoundCap));
const QVector<QPointF> points = QVector<QPointF>() << QPointF(x1, y1) << QPointF(x2, y2) << QPointF(x2, y2) << QPointF(x1, y3);
painter->setRenderHint(QPainter::Antialiasing);
painter->drawLines(points);

enter image description here

现在我发送了一个新的模拟器,我被要求实现一个可以在左上角看到的焦点图标。

两个请求略有不同,例如焦点图标用白色填充。

现在我正在考虑创建一个正方形并用白色填充它而不是在它的中间画两条线。这是一个好方法吗?有没有人之前做过这样的事情并有样品?

1 个答案:

答案 0 :(得分:0)

如果有人需要这样的东西,下面的代码创建了与问题中图片左上角相似的形状

QRectF newRec = rect;
painter->save();
newRec.setSize( newRec.size() / 20 * 11 );
painter->setBrush(Qt::white);

QColor colorRect( LMCTheme::Instance()->GetColor(InternalStuff::ColorIconInfoBarArrow));
painter->setPen(QPen( colorRect, 3, Qt::SolidLine, Qt::RoundCap));
painter->translate( rect.width()/2, rect.height()/10 );
painter->rotate(45);
painter->setRenderHint(QPainter::Antialiasing);

painter->drawRect(newRec);
painter->restore();

QColor color(LMCTheme::Instance()->GetColor(InternalStuff::ColorIconInfoBarBackground));
painter->setRenderHint(QPainter::Antialiasing);
painter->setCompositionMode(QPainter::CompositionMode_Clear);
painter->setPen(QPen(color, 3, Qt::SolidLine, Qt::RoundCap));
painter->drawLine( QLine(QPoint(rect.width()/2, rect.height()/10 ), QPoint(rect.width()/2, rect.height() - rect.height()/10 ) ) );
painter->drawLine( QLine(QPoint(rect.width()/10, rect.height()/2), QPoint(rect.width()*9/10, rect.height()/2)) );