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



  • 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.


  • Lifetime Qt Champion

    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.



  • 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.


  • Lifetime Qt Champion

    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.


Log in to reply
 

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