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. Weird QLabel behaviour when its text contains HTML syntax
Forum Updated to NodeBB v4.3 + New Features

Weird QLabel behaviour when its text contains HTML syntax

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 4.4k Views 1 Watching
  • 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.
  • N Offline
    N Offline
    napajejenunedk0
    wrote on 11 Sept 2012, 09:27 last edited by
    #1

    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.
    2) 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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 11 Sept 2012, 09:42 last edited by
      #2

      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 :)

      (Z(:^

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sierdzio
        Moderators
        wrote on 11 Sept 2012, 09:42 last edited by
        #3

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

        (Z(:^

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on 11 Sept 2012, 09:44 last edited by
          #4

          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.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            napajejenunedk0
            wrote on 11 Sept 2012, 11:07 last edited by
            #5

            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.

            1 Reply Last reply
            0
            • N Offline
              N Offline
              napajejenunedk0
              wrote on 11 Sept 2012, 11:13 last edited by
              #6

              <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?

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on 11 Sept 2012, 11:53 last edited by
                #7

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

                1 Reply Last reply
                0

                5/7

                11 Sept 2012, 11:07

                • Login

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