Results 1 to 4 of 4

Thread: QTextEdit <body> tags text only

  1. #1
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    5

    Default QTextEdit <body> tags text only

    I am storing some simple rich text (HTML) strings in a database which are input from a QTextEdit widget and using a QDataWidgetMapper.. I need the formatting attributes (bold, italic, underline, bullets) that rich text provides.

    Using the default implementation this means that each record stores the whole QTextDocument which is somewhat wasteful, as it probably only contains about 10 or so words of useful text. Given that all entries in the database will use the same DOCTYPE, CSS etc. I would prefer only to store the data between the <BODY> </BODY> tags.

    My idea is to subclass QTextEdit and implement a toHtmlBody() (or just reimplement toHtml()) however I haven't found a neat way of pulling just the body from the document.

    So far I tried QXmlStreamReader and various regex's I found on the web.

    Any ideas are most welcome!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QTextEdit <body> tags text only

    A fast way would be to reimp the toHtml() method and use regular expressions to filter all between the body tags. But then you still will have a lot of garbage. The best solution would probably be to create a custom method, and use QTextCursor to transform the text in the way you want. (only b and i tags for example)

  3. #3
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    5

    Default Re: QTextEdit <body> tags text only

    Thanks!

    A fast way would be to reimp the toHtml() method and use regular expressions to filter all between the body tags. But then you still will have a lot of garbage.
    Yep, this is what I am trying will keep looking for a suitable regular expression (if anyone has one it would be great!).

    I am interested though what other garbage I might have - so far I haven't seen anything except the tags that I am expecting but this might be because I am only typing and formatting, I guess 'pasting' could be a problem.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QTextEdit <body> tags text only

    if you allow pasting then you have to take care about a lot of stuff, so maybe it is an option for you to use e.g. HTML tidy.

Similar Threads

  1. Replies: 1
    Last Post: 6th December 2011, 22:44
  2. how to use html tags(rich text) in QTableWidgetHeaderItem
    By ansmehta in forum Qt Programming
    Replies: 0
    Last Post: 13th December 2010, 10:20
  3. QTextEdit + paste rich text as plain text only
    By Yong in forum Qt Programming
    Replies: 2
    Last Post: 6th February 2008, 16:45
  4. any body using qextserialport?
    By yagabey in forum Newbie
    Replies: 4
    Last Post: 25th December 2007, 22:08
  5. Getting All Text Between Tags in XML ?
    By seanmu13 in forum Qt Programming
    Replies: 1
    Last Post: 8th June 2007, 16:29

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.