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. Background for QStaticText by html

Background for QStaticText by html

Scheduled Pinned Locked Moved Solved General and Desktop
htmlcssqstatictext
3 Posts 2 Posters 464 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.
  • E Offline
    E Offline
    Ejok88
    wrote on 22 Nov 2022, 13:05 last edited by Ejok88
    #1

    Hello

    Environment: qt5.12.2, mingw64, windows 10 x64

    I get a simple example of widget with QStaticText (out of the documentation) and set the next text:

    #include <QWidget>
    #include <QStaticText>
    #include <QPainter>
    
    class MyWidget: public QWidget
    {
        Q_OBJECT;
    public:
        MyWidget(QString txt, QWidget *parent = 0) : QWidget(parent), m_staticText(txt) {}
    
    protected:
        void paintEvent(QPaintEvent *)
        {
            QPainter painter(this);
            painter.drawStaticText(0, 0, m_staticText);
        }
    
    private:
        QStaticText m_staticText;
    };
    
    auto styleStr = QString(R"(style = "background-color: %1;")").arg("red");
    auto formatedStr = QString("<pre %1>%2</pre>").arg(styleStr).arg("some text");
    MyWidget w(formatedStr);
    w.show();
    

    But then I see no background color (only text). When I change QStaticText to QLabel, it works properly. I can change the font for QStaticText, but background-color settings doesn't work..

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 22 Nov 2022, 13:24 last edited by
      #2

      QStaticText is an object that caches text layout information. As such it only accepts html formatting that refers to the text itself, not what it is drawn on (the background). If you want a background just get the text's size() and draw a colored rectangle before you draw the text.

      E 1 Reply Last reply 22 Nov 2022, 13:37
      2
      • C Chris Kawa
        22 Nov 2022, 13:24

        QStaticText is an object that caches text layout information. As such it only accepts html formatting that refers to the text itself, not what it is drawn on (the background). If you want a background just get the text's size() and draw a colored rectangle before you draw the text.

        E Offline
        E Offline
        Ejok88
        wrote on 22 Nov 2022, 13:37 last edited by
        #3

        @Chris-Kawa thanks 🙏

        1 Reply Last reply
        0

        1/3

        22 Nov 2022, 13:05

        • Login

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