Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsSimpleTextItem::paint() issue with colored SolidPen (found in Qt 5.13.2)
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 160 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lano1106
    wrote on last edited by lano1106
    #1

    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!

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      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();
      }
      

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved