Results 1 to 4 of 4

Thread: Get HTML selection from QWebPage

  1. #1
    Join Date
    Apr 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Get HTML selection from QWebPage

    QWebPage::selectedText() returns the selected text from a page. However, I would like to get the HTML. How can this be done?

    For example, if the user selects this HTML:

    The <b>quick brown fox</b> <img src="fox.jpg"/> jumps over the <em>lazy dog</em>.
    QWebPage::selectedText() returns:

    The quick brown fox jumps over the lazy dog.
    That is, no HTML tags. I would like to get the HTML code too.

    Thanks.

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Get HTML selection from QWebPage

    This seems quite difficult. Consider the HTML <tag>quick brown fox</tag>.

    The user selects the text "brown" in their browser. The copy routine might notice that <tag> has been applied to this text, but what should it do? Insert open and close tags around the selected text? That doesn't match the original document's HTML. How far ahead/behind should it search for the enclosing tags? What if those tags are nested within a larger enclosing block that applies some other property to the text?

    Going further: what about stylesheets? Should the copy routine reach outside the document itself and pull in this external information?

    Either way, you'll quickly run into cases where the copy is no longer a duplicate of the original source, or doesn't carry the information you're (probably) looking for. As such, there doesn't appear to be a unique solution to this problem; although you could still implement such a feature yourself, it would be unique to your particular requirements, and someone else might not be able to use it at all.

  3. #3
    Join Date
    Apr 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Get HTML selection from QWebPage

    Actually, I'm not planning to copy the selected text anywhere outside of its own document. I just want to modify it. Think of the typical bar with buttons for bold, italic, underlined and add link. When you select some text and click the Add Link button, after asking for the link target, the application takes the selection and wraps it between <a href="foo"> and </a>. That's exactly what I want.

    Thanks.

  4. #4
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Get HTML selection from QWebPage

    Yes, but you explicitly tell it you're looking for a link, so it has some information about what tags to look for and how to apply them. That's not what you originally asked - which was to select text from an HTML page and have the HTML tags it contains come along. As discussed above, this is a hard problem without a unique solution.

    If all you want to do is wrap selected text within a user-provided <a...></a> block, that's something you can trivially do on your end.

Similar Threads

  1. Plug-in widgets always on top in QWebPage
    By serras in forum Qt Programming
    Replies: 3
    Last Post: 29th July 2011, 03:22
  2. Unwanted downloads with QWebPage/QWebPage
    By ouekah in forum Newbie
    Replies: 6
    Last Post: 11th May 2010, 22:48
  3. How to use qwebpage in non-gui app
    By Cykus in forum Newbie
    Replies: 4
    Last Post: 10th May 2010, 11:35
  4. Can QWebPage see changes in a website
    By probine in forum Qt Programming
    Replies: 3
    Last Post: 15th August 2009, 23:23
  5. QwebPage within a QGraphicsWidget
    By Osprey in forum Qt Programming
    Replies: 0
    Last Post: 16th June 2009, 17:32

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.