Important: Please read the Qt Code of Conduct -

QGraphicsTextItem - paint and html

  • Hi there,

    I have subclassed QGraphicsTextItem and I want to put a semi transparent box behind the HTML formatted text. But doing the following, in the paint routine, doesn't work:


    If I use plainText, there is no problem drawing a rectangle behind the text. But when I use setHTML, the box is not drawn. Am I missing something or is this something that can't be done in a subclassed QGraphicsTextItem? Any pointers would be appreciated.

    Best wishes (and seasons greetings)

  • Lifetime Qt Champion

    Can it be something in the HTML ?
    If i use the Diagram example and use setHTML its still transparent.

    alt text

    auto html =
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "">
        <meta name="qrichtext" content="1" />
        <style type="text/css">
           p, li { white-space: pre-wrap; }
     <body style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;">
        <p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" text-decoration: underline;">This</span> is <span style=" font-size:14pt; font-weight:600;">HTML</span><span style=" font-style:italic;">text</span></p>
        DiagramTextItem* item = qgraphicsitem_cast<DiagramTextItem*>(selectedItems().first());
        item->setHtml(html) ;

  • @mrj interesting. I will check that example. Thanks for the pointer...

    Best wishes

  • Thank you mrjj. That will help me do what I want. I had never looked at the Diagram example - just goes to show, the information is often right under your nose.

    Thanks again and have a very merry and happy Christmas

    Best wishes

  • Lifetime Qt Champion

    I often suggest that sample if people want to make any kind of diagram editor as its a good start.
    Yeah, every now and then our local Qt Lord suggests some class i never saw before :)

    Merry Christmas and a glorious new year

Log in to reply