[Interest] Draw ellipse (and etc.) the same way as cosmetic QPen

Dmitrii Volosnykh dmitrii.volosnykh at gmail.com
Tue Jan 14 13:11:48 CET 2014


Is there a possibility to draw something irrespectively to scene
transformations such as cosmetic QPen?

I've tried to approaches but none brings me to a success. I have some scene
that may contain a polyline with vertices represented by bold dots. While
polyline and thus coordinates of dots should respect scene scaling, I don't
want the size of dots to change.

Here's the Polyline's imlementation of paint() method (Polyline inherits
QGraphicsPolygonItem):

void PolylineItem::paint(QPainter* const painter, const
QStyleOptionGraphicsItem* const option, QWidget* const widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    painter->setPen(pen());
    painter->setBrush(brush());

    painter->drawPolyline(polygon());

#if 0
    if (polygon().size() > m_points.size()) {
        for (int i = m_points.size(); i < polygon().size(); ++i) {
            auto* const point = new QGraphicsEllipseItem(this);
            point->setPen(pen());
            point->setBrush(brush());
//            point->setFlag(QGraphicsItem::ItemIgnoresTransformations);
            m_points.append(point);
        }
    } else if (polygon().size() < m_points.size()) {
        const int count = m_points.size() - polygon().size();
        for (int i = 0; i < count; ++i) {
            delete m_points.takeLast();
        }
    }

    const qreal r = pen().widthF() * POINT_RADIUS_RATIO;
    for (int i = 0; i < m_points.size(); ++i) {
        const QPointF& c = polygon().at(i);
        m_points[i]->setRect(c.x() - r, c.y() - r, 2 * r, 2 * r);
    }
#else
    const qreal r = pen().widthF() * POINT_RADIUS_RATIO;
    foreach (const QPointF& point, polygon()) {
        painter->drawEllipse(point, r, r);
    }
#endif
}

The code between #else and #endif directives is the first straightforward
way I've tried. The points are scaled in size.
The part between #if and #else behaves the same when point->setFlag() is
commented out. If I enable this invokation, the points are constantly of
the same size, as I want it, but their positions are scaled outside the
polyline. In fact, they are not scaled, but the whole scene with the
polyline itself are scaled, so it looks like the dots are moving outside.

Can't figure out what to do. Asking for help.

Regards,
Dmitrii.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140114/6e7519cd/attachment.html>


More information about the Interest mailing list