Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QGraphicsSimpleTextItem::paint() issue with colored SolidPen (found in Qt 5.13.2)



  • I want to overlay text in a derived class from QChart. I have stumbled on the problem that as soon as I want to color the text, the text becomes thicker than its black counterpart:

    The default NoPen pen

    Setting a colored SolidLine pen:
    colored SolidLine pen

    this is harder to read and this behavior did annoy me a lot. I have nailed down the problem to QGraphicsSimpleTextItem::paint() code. I did override the paint implementation to something that is braindead stupid that does what I need and it fix my issue:

    class StupidSimpleText : public QGraphicsSimpleTextItem
    {    
    public:
        StupidSimpleText(const QString &text, QGraphicsItem *parent = NULL);
        StupidSimpleText(QGraphicsItem *parent = NULL);
        
        virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    };
    
    void StupidSimpleText::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        Q_UNUSED(widget);
    
        painter->setFont(font());
        painter->setPen(pen());
        QFontMetrics metrics(font());
        QSize textSize  = metrics.size(Qt::TextSingleLine, text());
        painter->drawText(QRectF(QPointF(0,0), textSize), Qt::AlignLeft|Qt::AlignVCenter, text());
    }
    
    void LinkedChart::setInfoText(const QString &text)
    {
        if (text == m_text)
            return;
        m_text = text;
        if (!m_textItem) {
            if (!text.size()) return;
    //        m_textItem = scene()->addSimpleText(text, m_textFont);
            m_textItem = new StupidSimpleText(text);
            scene()->addItem(m_textItem);
            QPen magentaPen = QPen(QColor(Qt::magenta), 1);
            m_textItem->setPen(magentaPen);
            QPointF plotTopLeft = plotArea().topLeft();
            plotTopLeft.rx() += 5.0;
            plotTopLeft.ry() += 5.0;
            m_textItem->setPos(plotTopLeft);
    
            /*
             * Connection to reposition the graphics item when/if the plotarea is changing.
             */
            connect(this, SIGNAL(plotAreaChanged(const QRectF &)),
                    this, SLOT(ourPlotAreaChanged(const QRectF &)));
        }
        m_textItem->setText(text);
    }
    

    Honestly, I don't fully understand everything QGraphicsSimpleTextItem is doing but the result of StupidSimple text is proving that something is wrong with QGraphicsSimpleTextItem::paint():
    StupidSimple text class output

    I hope this helps you guys!


  • Lifetime Qt Champion

    My testcase works fine for me with 5.13 and 5.14/dev:

    #include <QtWidgets>
    
    class GraphicsSimpleTextItem : public QGraphicsSimpleTextItem
    {    
    public:
        using QGraphicsSimpleTextItem::QGraphicsSimpleTextItem;
        void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
    };
    
    void GraphicsSimpleTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        Q_UNUSED(widget);
        painter->setFont(font());
        painter->setPen(pen());
        QFontMetrics metrics(font());
        QSize textSize  = metrics.size(Qt::TextSingleLine, text());
        painter->drawText(QRectF(QPointF(0,0), textSize), Qt::AlignLeft|Qt::AlignVCenter, text());
    }
    
    int main(int argc, char **argv)
    {
        QApplication app(argc, argv);
        QGraphicsScene scene;
        QGraphicsView view(&scene);
        view.show();
        GraphicsSimpleTextItem item("Test");
        item.setPen(QPen(QColor(Qt::magenta), 1));
        scene.addItem(&item);
        return app.exec();
    }
    

Log in to reply