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. QTextBrowser::setHtml() doesn't change the html
Forum Updated to NodeBB v4.3 + New Features

QTextBrowser::setHtml() doesn't change the html

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.7k 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.
  • H Offline
    H Offline
    Hertzy
    wrote on 2 Jul 2014, 13:02 last edited by
    #1

    I'm writing a program where MainWindow::keyPressEvent() compares the typed character with the expected input, and changes the contents of a textbrowser accordingly. The idea is to append the correct letter in red if the wrong character was typed, and in black if the correct character was typed.

    I have hit a frustrating snag, namely that TextBrowser::setHtml() does not set the html.

    This code, in void MainWindow::keyPressEvent(QKeyEvent *e):
    @
    qDebug()<<html<<"\n\n";
    ui->textBrowser->setHtml(html);
    qDebug()<<ui->textBrowser->toHtml()<<"\n";
    @

    Gives this output:
    @
    "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
    <html><head><meta name="qrichtext" content="1" /><style type="text/css">
    p, li { white-space: pre-wrap; }
    </style></head><body >
    <p ><font color="red">H</font></p></body></html>"

    "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
    <html><head><meta name="qrichtext" content="1" /><style type="text/css">
    p, li { white-space: pre-wrap; }
    </style></head><body >
    <p ><br /></p></body></html>"
    @

    As you can see, the textbrowser's html content is different from what was set as the textBrowser's content in the previous line. I have no idea why this happens.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 2 Jul 2014, 14:59 last edited by
      #2

      Hi and welcome to devnet,

      AFAIK, toHtml does a conversion from the content of the QTextBrowser's QTextEdit's QTextDocument so it does not return exactly the same document you set.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Hertzy
        wrote on 2 Jul 2014, 16:48 last edited by
        #3

        I see. The original problem that led me to notice this issue was that nothing was added to the text browser before a correct letter was typed.

        I've found that the parser is rejecting adding "<font color="red">H</font>", with this code,

        @
        int pidx=html.lastIndexOf("</p>");
        html.insert(pidx,toAdd);
        @

        Unless a regular letter without the font tags has been inserted before.

        The output looks like
        @
        "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
        <html><head><meta name="qrichtext" content="1" /><style type="text/css">
        p, li { white-space: pre-wrap; }
        </style></head><body >
        <p ><br /><font color="red">H</font></p></body></html>"

        "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
        <html><head><meta name="qrichtext" content="1" /><style type="text/css">
        p, li { white-space: pre-wrap; }
        </style></head><body >
        <p ><br /></p></body></html>"
        @

        The oddest thing is that

        @
        QString html=ui->textBrowser->toHtml();
        if(html.contains("<br />")){
        html="<p>"+toAdd+"</p>";
        }else{
        int pidx=html.lastIndexOf("</p>");
        html.insert(pidx,toAdd);
        }
        @

        works perfectly. I think this is a bug.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 2 Jul 2014, 21:17 last edited by
          #4

          The parser is not perfect but can be enhanced. You should have a look a the "bug report system":http://bugreports.qt-project.org to see if this already known, if not then you can consider opening a new report providing a minimal compilable example that shows the behavior.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          1/4

          2 Jul 2014, 13:02

          • Login

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