Qt6 topics merged with General

[Solved] outline on a QGraphicsTextItem

  • Hello,

    i am using QGraphicsTextItem on a QGraphicsScene, i would like to have my QGraphicsTextItem as outline text, i cannot find a way to achieve that... Anyone has any idea how to do it.

    Thank you for your help

  • Without re-implementing the paint method?! hum, I'm afraid not...

    However, you can you use "QGraphicsSimpleTextItem":http://doc.trolltech.com/4.6/qgraphicssimpletextitem.html?
    If so, you can use setPen to set the outline color and setBrush to transparent.

  • hello thank you for your answer...

    well QGrahicsSimpleTextItem, is not rich text and does not let me reedit the text.

    So reimplementing the paint method is what i am trying to figure out but unfortunately i cannot see how to grab the text to addit to a QPainterPath (if this mecanism can work!)

  • i have been helped to solve that, it works, you need to have the appropriate QCharFormat setup to the QTextCursor of your QGraphicstextitem

  • Can you post the code here?

  • Here's the code.
    A widget is created in designer with a QGraphicsView.
    A QGraphicsScene pointer is defined as a private member of the widget.
    A QTextDocument is defined as a private member of the widget (although that is not necessary, the document of the QGraphicsTextItem can be used too)
    A QGraphicsTextItem is defined as a private member of the widget.


    scene = new QGraphicsScene(ui->graphicsView);

    document = new QTextDocument;

    QTextCharFormat charFormat;
    charFormat.setFont(QFont("times", 24));

    QPen outlinePen = QPen (QColor(255, 0, 0), 1, Qt::SolidLine);

    QTextCursor cursor = QTextCursor(document);
    cursor.insertText("Test", charFormat);

    textItem = new QGraphicsTextItem();


  • Thanks for the code. =)

    I've just asked that because probably someone will have the same problem in the future and a "I solved it" message will not help them =)

  • Thank you for posting the code. It helps me quite a bit.

Log in to reply