Search code examples
c++qtqwidget

Qt balloon widget mask


I've created a balloon widget for my application. The problem is that the tip of the widget is jagged, so what am I doing wrong?

Jagged tip

void BalloonWidget::paintEvent(QPaintEvent *)
{   
    QVector<QPointF> vertices;
    vertices << QPointF(0, 0)
             << QPointF(width(), 0)
             << QPointF(width(), height() * 0.8)
             << QPointF(width() * 0.60, height() * 0.8)
             << QPointF(width() * 0.5, height())
             << QPointF(width() * 0.40, height() * 0.8)
             << QPointF(0, height() * 0.8);

    balloonPoly = QPolygonF(vertices);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setRenderHint(QPainter::HighQualityAntialiasing, true);
    painter.setBrush(QBrush(backgroundColor));

    QRegion maskRegion(balloonPoly.toPolygon(), Qt::WindingFill);  
    painter.drawPolygon(balloonPoly);
    setMask(maskRegion);
}

Solution

  • I've found the reason for the jaggedness. And I was wrong with my asumption that I have to mask the polygon. If you use antialiasing and only want a 1px (cosmetic pen) pen the vertices coordinates have to be 'shifted' by 0.5px And width/height have to be uneven. So I subtracted 0.5 from my width and height. Now it's perfectly smooth