Displaying HTML codes in QLabel



  • Hello All,

    I am getting html codes embedded in the xml from the server for example:
    @"Robert Glück ; drawings by Edward Aulerich"@ where ü is getting displayed as &<no space>#252; and when i set it to the QLabel the html code doesn't get encoded. Is there a way by which i can display it correctly?

    Thanks,

    Sri

    PS: i gave <no space> coz the text edit is converting it to ü Please remove <no space> to get he exact code.



  • You can try to wrap it in a pair of <html></html> tags or force QLabel to interpret the text as HTML by using QLabel::setTextFormat(Qt::RichText)



  • Hi Andre,

    Thanks a lot for the help. I have one more problem where i have to display the same text on a QListWidget but according to doc RichText can be applied only to widgets which support RichText. So, i did the following:
    @QStaticText text;
    text.setTextFormat(Qt::RichText);
    text.setText("ü");@

    And set the same to QListWidgetItem but, i realized that QListWidgetItem doens't support RichText. Do i have create a QLabel and place it as QListWidgetItem using setWidgetItem or is there any another workaround?

    Thanks,

    Sri



  • QListViews by default do not support rich text, but they do support unicode. That means that you can display the whole character set, including unlauts, ringel-S, and basically anything else you can come up with. I am not sure if there is Qt API to convert HTML escape sequences to unicode for a text, but if not, it would not be hard to make yourself.

    I would not go mucking about with putting labels on an item view just for this.

    Edit: perhaps you can abuse QTextDocument for the translation from HTML to unicode.



  • Hello Andre,

    Thanks once again and i did the following:
    @QPointer<QTextDocument> doc = new QTextDocument();
    doc->setHtml(mBookDetail->authorsList.at(index));
    QListWidgetItem* item = new QListWidgetItem(doc->toPlainText());
    mAuthorsList->insertItem(index,item );
    delete doc;@

    I will take up writing an API to convert HTML escape sequences to unicode for a text.

    I have one Request, Shouldn't there be a sub-category in the forum which should be something like "Qt General" because my doubt was on Qt irrespective of which platform i am working on.

    Thanks,

    Sri



  • I would not create/destroy the QTextDocument for each item, but otherwise, I'm glad it works. Just convert the text once (on loading, for example) using the same QTextDocument instance. Also, using a QPointer is not really needed here.

    The forum structure request is better put in the Beta Testing forum, or better yet, on Jira.



  • Thanks for the advice. Also, i have the raised the question in Beta Testing Forum.

    Thanks,

    Sri


Log in to reply
 

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