Important: Please read the Qt Code of Conduct -

[solved] Applying stylesheets to HTML tags in QLabel

  • This is probably a really simple question, but I have not found any text describing this.

    I have the following piece of code:

    QLabel *label = new QLabel("<h1>Header</h1><p>some text</p>");

    Is it possible to apply a stylesheet to the h1-tag? I've tried

    QLabel::h1 {
    color: #123456;

    But this does not work.

    What is the best way to apply stylesheets to parts of the text inside a QLabel?

    I might add that I'm not that experienced in stylesheets, I mostly do embedded programming.

  • No, style sheets are not supported here. Qt supports a "very limited subset":/doc/qt-4.8/richtext-html-subset.html of HTML outside of the actual webkit classes. -Style sheets are not part of that.- Styles sheets are only supported in a very limited way.

  • OK, then I'll solve it some other way.

    Thanks :)

  • As Andre says "Qt supports a very limited subset of HTML outside of the actual webkit classes." But you can stil add simple styles in QLabel's html:
    @QLabel *label = new QLabel("<h1 style = color:red >Header</h1><p>some text</p>");@

  • That was actually my first solution, but I wanted to place as much of the style-setting in the CSS file. I think I'll split the information into several QLabels instead, and control each label with the stylesheet.

Log in to reply