PDA

View Full Version : QTextBrowser go to an anchor



bred
30th September 2010, 15:05
Given an HTML code like this:


<H1>Table of Contents</H1>
<P><A href="#section1">Introduction</A><BR>
<A href="#section2">Some background</A><BR>
<A href="#section2.1">On a more personal note</A><BR>
...the rest of the table of contents...
...the document body...
<H2><A name="section1">Introduction</A></H2>
...section 1...
<H2><A name="section2">Some background</A></H2>
...section 2...

We have a QTextBrowser object:


QTextBrowser textb ;
......
textb.setHtml( html_with_anchor ) ;
textb.setOpenLinks ( false ) ;

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.

tbscope
30th September 2010, 15:09
http://doc.qt.nokia.com/4.7/qtextbrowser.html#anchorClicked

bred
30th September 2010, 16:29
http://doc.qt.nokia.com/4.7/qtextbrowser.html#anchorClicked

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

tbscope
30th September 2010, 16:35
void MyClass:goToAnchor(const QString &anchorName)
{

QUrl anchorUrl(baseUrl + "#" + anchorName);

textBrowser.setSource(anchorUrl);
}

Something like that.

Edit: Changed textBrowser.source().toString() to baseUrl (which is a qstring holding the base url).
Otherwise it will not work.

bred
30th September 2010, 16:58
Not exactly.

I want to create a new url directly from a QString

in pseudo code is:


QSrting my_html_page = build_html() ;
QUrl url = crante_a_new_url( given my_html_page ) ;
url.setFragment( my_anchor ) ;
textbrowser.setSource( url ) ;

Tnx