toHTML without header?



  • Hi,

    I'm new to C++ and Qt and I'm writing some code where I create an html document containing text from a couple of different widgets. So I'd like to retrieve the html string from a QTextEdit without the headers and footers. So without the "<!DOCTYPE HTML PUBLIC" etc... (because I'm including those myself) but including all tags pertaining to the string itself (like colors etc...).

    Is this possible without manually replacing everything I don't need?

    Thanks in advance.


  • Qt Champions 2016

    Hi
    As far as I know, you can only get the full html.



  • I did not test it but you could try iterating the text blocks:

    QTextDocument* doc= textEdit->document();
      for (QTextBlock it = doc->begin(); it != doc->end(); it = it.next())
            qDebug() << it.text();
    


  • @VRonin
    No, that just returns plain text, unfortunately.



  • @angela2016 You're going to have to strip out the html you don't want. Your only 2 options from that control are text() and toHtml(). It should be nice and easy since you know what you added so just use a QString to remove the stuff you don't want.

    I.e.

    auto text = textEdit->toHtml();
    text = text.remove(myHeader);
    text = text.remove(myFooter);
    

    Be careful though as remove will remove all instances so if your header is something simple like "<div>" it will remove all "<div>"'s that it can find in the string.



  • Yes, I guess I'll add some function to do that.

    But I don't understand what you mean by "header you wrote". Is there a way to insert your own header into QTextDocument? Because the one I'm talking about is generated automatically.



  • @angela2016 said in toHTML without header?:

    Yes, I guess I'll add some function to do that.

    But I don't understand what you mean by "header you wrote". Is there a way to insert your own header into QTextDocument? Because the one I'm talking about is generated automatically.

    Oh I thought it was a custom one you were adding. And yes you can change any html in any control in Qt. :) If it's the Qt generated html you could just leave it alone, maybe I don't understand your desire to not have the header returned.

    But you can definitely modify the outputted html to suit your needs if you don't want the full html for some reason. You can use my method, or if you are going to do it a lot just derive your own control and override the toHtml() function to strip the parts you don't want.


Log in to reply
 

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