Results 1 to 5 of 5

Thread: QTextBrowser go to an anchor

  1. #1
    Join Date
    Sep 2010
    Posts
    45
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTextBrowser go to an anchor

    Given an HTML code like this:

    Qt Code:
    1. <H1>Table of Contents</H1>
    2. <P><A href="#section1">Introduction</A><BR>
    3. <A href="#section2">Some background</A><BR>
    4. <A href="#section2.1">On a more personal note</A><BR>
    5. ...the rest of the table of contents...
    6. ...the document body...
    7. <H2><A name="section1">Introduction</A></H2>
    8. ...section 1...
    9. <H2><A name="section2">Some background</A></H2>
    10. ...section 2...
    To copy to clipboard, switch view to plain text mode 

    We have a QTextBrowser object:

    Qt Code:
    1. QTextBrowser textb ;
    2. ......
    3. textb.setHtml( html_with_anchor ) ;
    4. textb.setOpenLinks ( false ) ;
    To copy to clipboard, switch view to plain text mode 

    It's possible give an external order to the QTextBrowser object to scroll to an anchor, without any mouse click.

    I want a function like this:
    textb. GoToAnchor( section2 )

    Tnx.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

  3. #3
    Join Date
    Sep 2010
    Posts
    45
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextBrowser go to an anchor

    It's possible to create a QUrl directly from a QString that store some html code.

    For example:
    QString html_text = create_a_text() ;
    QUrl = .... created from html_text

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QTextBrowser go to an anchor

    Qt Code:
    1. void MyClass:goToAnchor(const QString &anchorName)
    2. {
    3.  
    4. QUrl anchorUrl(baseUrl + "#" + anchorName);
    5.  
    6. textBrowser.setSource(anchorUrl);
    7. }
    To copy to clipboard, switch view to plain text mode 

    Something like that.

    Edit: Changed textBrowser.source().toString() to baseUrl (which is a qstring holding the base url).
    Otherwise it will not work.
    Last edited by tbscope; 30th September 2010 at 16:49.

  5. #5
    Join Date
    Sep 2010
    Posts
    45
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextBrowser go to an anchor

    Not exactly.

    I want to create a new url directly from a QString

    in pseudo code is:

    Qt Code:
    1. QSrting my_html_page = build_html() ;
    2. QUrl url = crante_a_new_url( given my_html_page ) ;
    3. url.setFragment( my_anchor ) ;
    4. textbrowser.setSource( url ) ;
    To copy to clipboard, switch view to plain text mode 

    Tnx

Similar Threads

  1. Replies: 4
    Last Post: 4th December 2010, 01:52
  2. Replies: 0
    Last Post: 24th September 2010, 05:37
  3. Anchor widgets in QGraphicsScene
    By johnsoga in forum Qt Programming
    Replies: 4
    Last Post: 18th February 2010, 15:25
  4. QWebView and scrolling to anchor
    By serenti in forum Qt Programming
    Replies: 1
    Last Post: 16th July 2009, 16:55
  5. Replies: 2
    Last Post: 15th March 2006, 12:13

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.