Weird QLabel behaviour when its text contains HTML syntax



  • main.cpp
    @
    #include <QApplication>
    #include <QLabel>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    #define USE_HTML_SYNTAX
    #ifdef USE_HTML_SYNTAX
    const QString last = "<a >link</a>";
    #else
    const QString last = "link";
    #endif
    QLabel label( QString("link:%1link:%2link:%3%4")
    .arg( QString() )
    .arg( QChar( QChar::LineSeparator ) )
    .arg( QChar( QChar::ParagraphSeparator ) )
    .arg( last ) );
    label.setOpenExternalLinks( true );
    label.setTextInteractionFlags( label.textInteractionFlags()
    | Qt::LinksAccessibleByKeyboard
    | Qt::LinksAccessibleByMouse );
    label.show();

    return a.exec&#40;&#41;;
    

    }
    @

    Run this code as it is. The result is:
    link:link: link:
    link:

    Now comment out the preprocessor statement:
    @
    #define USE_HTML_SYNTAX
    @

    and run the code - enlarge the label using the size grip at the bottom-right, since the contents don't fit.
    The result is:
    link:link:
    link:
    link:

    Conclusion:
    1) Using HTML syntax:
    The line separator character is treated as space.
    The paragraph separator character is treated as newline.

    1. Not using HTML syntax inside the label's text:
      The line separator character is treated as newline.
      The paragraph separator character is treated as newline.

    Where does this difference come from?

    This happens only when using Qt in Windows environment.
    I am using Qt 4.8.2 on Windows 7 x64, Linux Mint 13 (Maya) Xfce x86, Mac OS X Lion 10.7.3.


  • Moderators

    Qt uses different engines to render the text if it detects HTML presence. You can force it to use plain text or rich text, but not both :)


  • Moderators

    Here is a link to docs entry about it: "link":http://qt-project.org/doc/qt-4.8/qlabel.html#textFormat-prop.



  • Sounds to me like it is comming from HTML itself. In HTML, line breaks are done with tags like <br> and <p>. The use of HTML-like tags triggers interpretting the whole string as HTML, and in HTML, your own Unicode line breaks or paragraphs separators (should) count for nothing. Not sure why paragraph separator seems to be accepted though. I think it should be ignored or treated like white space like the line separator, but I am not sure. Check the HTML specs for that, and remember that Qt's (non-webkit) HTML support is only a small subset of HTML. It is more of a simple rich text text that happens to follow the same syntax as HTML, but it does not claim to be fully compliant nor feature complete.



  • bq. Sounds to me like it is comming from HTML itself. In HTML, line breaks are done with tags like <br> and <p>. The use of HTML-like tags triggers interpretting the whole string as HTML ...

    That is what I first thought - that if a single piece of HTML syntax is detected in the text, the whole string gets interpreted as HTML.



  • <br> and <p> work as expected. So, what is the correct approach for inserting newline characters when there is a mixture of plain and rich HTML text in a QLabel's text? Use <br> I suppose?



  • Either use all HTML, or use plain text only, I'd say.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.