QTextBrowser Class Reference

#include <qtextbrowser.h>

Inheritance diagram for QTextBrowser:

Inheritance graph
[legend]
Collaboration diagram for QTextBrowser:

Collaboration graph
[legend]
List of all members.

Detailed Description

The QTextBrowser class provides a rich text browser with hypertext navigation.

This class extends QTextEdit (in read-only mode), adding some navigation functionality so that users can follow links in hypertext documents.

If you want to provide your users with an editable rich text editor, use QTextEdit. If you want a text browser without hypertext navigation use QTextEdit, and use QTextEdit::setReadOnly() to disable editing. If you just need to display a small piece of rich text use QLabel.

The contents of QTextEdit are set with setHtml() or setPlainText(), but QTextBrowser also implements the setSource() function, making it possible to use a named document as the source text. The name is looked up in a list of search paths and in the directory of the current document factory. If a document name ends with an anchor (for example, "\c #anchor"), the text browser automatically scrolls to that position (using scrollToAnchor()). When the user clicks on a hyperlink, the browser will call setSource() itself with the link's \c href value as argument. You can track the current source by connecting to the sourceChanged() signal. @section Navigation QTextBrowser provides backward() and forward() slots which you can use to implement Back and Forward buttons. The home() slot sets the text to the very first document displayed. The anchorClicked() signal is emitted when the user clicks an anchor. To override the default navigation behavior of the browser, call the setSource() function to supply new document text in a slot connected to this signal. If you want to load documents stored in the Qt resource system use \c{qrc} as the scheme in the URL to load. For example, for the document resource path \c{:/docs/index.html} use \c{qrc:/docs/index.html} as the URL with setSource(). \sa QTextEdit, QTextDocument Definition at line 38 of file qtextbrowser.h.

Public Slots

virtual void setSource (const QUrl &name)
virtual void backward ()
virtual void forward ()
virtual void home ()
virtual void reload ()

Signals

void backwardAvailable (bool)
void forwardAvailable (bool)
void sourceChanged (const QUrl &)
void highlighted (const QUrl &)
void highlighted (const QString &)
void anchorClicked (const QUrl &)

Public Member Functions

 QTextBrowser (QWidget *parent=0)
virtual ~QTextBrowser ()
QUrl source () const
QStringList searchPaths () const
void setSearchPaths (const QStringList &paths)
virtual QVariant loadResource (int type, const QUrl &name)
bool isBackwardAvailable () const
bool isForwardAvailable () const
void clearHistory ()
bool openExternalLinks () const
void setOpenExternalLinks (bool open)

Protected Member Functions

bool event (QEvent *e)
virtual void keyPressEvent (QKeyEvent *ev)
virtual void mouseMoveEvent (QMouseEvent *ev)
virtual void mousePressEvent (QMouseEvent *ev)
virtual void mouseReleaseEvent (QMouseEvent *ev)
virtual void focusOutEvent (QFocusEvent *ev)
virtual bool focusNextPrevChild (bool next)
virtual void paintEvent (QPaintEvent *e)

Private Member Functions

 Q_PRIVATE_SLOT (d_func(), void _q_documentModified()) Q_PRIVATE_SLOT(d_func()
void _q_activateAnchor (const QString &)) Q_PRIVATE_SLOT(d_func()


Constructor & Destructor Documentation

QTextBrowser::QTextBrowser ( QWidget parent = 0  )  [explicit]

Constructs an empty QTextBrowser with parent parent.

Definition at line 445 of file qtextbrowser.cpp.

References d.

00446     : QTextEdit(*new QTextBrowserPrivate, parent)
00447 {
00448     Q_D(QTextBrowser);
00449     d->init();
00450 }

QTextBrowser::~QTextBrowser (  )  [virtual]

Definition at line 469 of file qtextbrowser.cpp.

00470 {
00471 }


Member Function Documentation

QUrl QTextBrowser::source (  )  const

Definition at line 492 of file qtextbrowser.cpp.

References d.

Referenced by HelpDialog::addBookmark(), getPixmap(), HelpWindow::hasAnchorAt(), MainWindow::on_actionSaveAs_triggered(), MainWindow::saveSettings(), MainWindow::showLinks(), and MainWindow::showSearchLink().

00493 {
00494     Q_D(const QTextBrowser);
00495     if (d->stack.isEmpty())
00496         return QUrl();
00497     else
00498         return d->stack.top().url;
00499 }

QStringList QTextBrowser::searchPaths (  )  const

Definition at line 509 of file qtextbrowser.cpp.

References d.

Referenced by FeatureTextBrowser::FeatureTextBrowser().

00510 {
00511     Q_D(const QTextBrowser);
00512     return d->searchPaths;
00513 }

void QTextBrowser::setSearchPaths ( const QStringList paths  ) 

Definition at line 515 of file qtextbrowser.cpp.

References d.

Referenced by TabbedBrowser::createHelpWindow(), and FeatureTextBrowser::FeatureTextBrowser().

00516 {
00517     Q_D(QTextBrowser);
00518     d->searchPaths = paths;
00519 }

QVariant QTextBrowser::loadResource ( int  type,
const QUrl name 
) [virtual]

This function is called when the document is loaded. The type indicates the type of resource to be loaded. For each image in the document, this function is called once.

The default implementation ignores type and tries to locate the resources by interpreting name as a file name. If it is not an absolute path it tries to find the file in the paths of the searchPaths property and in the same directory as the current source. On success, the result is a QVariant that stores a QByteArray with the contents of the file.

If you reimplement this function, you can return other QVariant types. The table below shows which variant types are supported depending on the resource type:

ResourceType QVariant::Type QTextDocument::HtmlResource QString or QByteArray QTextDocument::ImageResource QImage, QPixmap or QByteArray QTextDocument::StyleSheetResource QString or QByteArray

Reimplemented from QTextEdit.

Definition at line 829 of file qtextbrowser.cpp.

References QFile::close(), d, data, name, QFile::open(), qWarning(), QIODevice::readAll(), and QIODevice::ReadOnly.

00830 {
00831     Q_D(QTextBrowser);
00832 
00833     QByteArray data;
00834     QString fileName = d->findFile(d->resolveUrl(name));
00835     QFile f(fileName);
00836     if (f.open(QFile::ReadOnly)) {
00837         data = f.readAll();
00838         f.close();
00839     } else {
00840         qWarning("QTextBrowser: Cannot open '%s' for reading", name.toString().toLocal8Bit().data());
00841         return QVariant();
00842     }
00843 
00844     return data;
00845 }

Here is the call graph for this function:

bool QTextBrowser::isBackwardAvailable (  )  const

Since:
4.2
Returns true if the text browser can go backward in the document history using backward().

See also:
backwardAvailable(), backward()

Definition at line 855 of file qtextbrowser.cpp.

References d.

Referenced by MainWindow::browserTabChanged().

00856 {
00857     Q_D(const QTextBrowser);
00858     return d->stack.count() > 1;
00859 }

bool QTextBrowser::isForwardAvailable (  )  const

Since:
4.2
Returns true if the text browser can go forward in the document history using forward().

See also:
forwardAvailable(), forward()

Definition at line 869 of file qtextbrowser.cpp.

References d.

Referenced by MainWindow::browserTabChanged().

00870 {
00871     Q_D(const QTextBrowser);
00872     return !d->forwardStack.isEmpty();
00873 }

void QTextBrowser::clearHistory (  ) 

Since:
4.2
Clears the history of visited documents and disables the forward and backward navigation.

See also:
backward(), forward()

Definition at line 883 of file qtextbrowser.cpp.

References backwardAvailable(), d, emit, and forwardAvailable().

00884 {
00885     Q_D(QTextBrowser);
00886     d->forwardStack.clear();
00887     if (!d->stack.isEmpty())
00888         d->stack.resize(1);
00889     emit forwardAvailable(false);
00890     emit backwardAvailable(false);
00891 }

bool QTextBrowser::openExternalLinks (  )  const

Definition at line 904 of file qtextbrowser.cpp.

References d.

00905 {
00906     Q_D(const QTextBrowser);
00907     return d->openExternalLinks;
00908 }

void QTextBrowser::setOpenExternalLinks ( bool  open  ) 

Definition at line 910 of file qtextbrowser.cpp.

References d.

00911 {
00912     Q_D(QTextBrowser);
00913     d->openExternalLinks = open;
00914 }

void QTextBrowser::setSource ( const QUrl name  )  [virtual, slot]

Definition at line 532 of file qtextbrowser.cpp.

References backwardAvailable(), d, emit, forwardAvailable(), QTextBrowserPrivate::HistoryEntry::hpos, QUrl::isValid(), QTextBrowserPrivate::HistoryEntry::url, and QTextBrowserPrivate::HistoryEntry::vpos.

Referenced by forward(), home(), reload(), FeatureTextBrowser::setSource(), and HelpWindow::setSource().

00533 {
00534     Q_D(QTextBrowser);
00535 
00536     int hpos = d->hbar->value();
00537     int vpos = d->vbar->value();
00538 
00539     d->setSource(url);
00540 
00541     if (!url.isValid())
00542         return;
00543 
00544     if (!d->stack.isEmpty() && d->stack.top().url == url) {
00545         // the same url you are already watching
00546     } else {
00547         if (!d->stack.isEmpty()) {
00548             d->stack.top().hpos = hpos;
00549             d->stack.top().vpos = vpos;
00550         }
00551         QTextBrowserPrivate::HistoryEntry entry;
00552         entry.url = url;
00553         entry.hpos = 0;
00554         entry.vpos = 0;
00555         d->stack.push(entry);
00556 
00557         emit backwardAvailable(d->stack.count() > 1);
00558 
00559         if (!d->forwardStack.isEmpty() && d->forwardStack.top().url == url) {
00560             d->forwardStack.pop();
00561             emit forwardAvailable(d->forwardStack.count() > 0);
00562         } else {
00563             d->forwardStack.clear();
00564             emit forwardAvailable(false);
00565         }
00566     }
00567 }

void QTextBrowser::backward (  )  [virtual, slot]

Changes the document displayed to the previous document in the list of documents built by navigating links. Does nothing if there is no previous document.

See also:
forward(), backwardAvailable()

Definition at line 631 of file qtextbrowser.cpp.

References backwardAvailable(), d, emit, and forwardAvailable().

Referenced by TabbedBrowser::backward(), and keyPressEvent().

00632 {
00633     Q_D(QTextBrowser);
00634     if (d->stack.count() <= 1)
00635         return;
00636     d->forwardStack.push(d->stack.pop());
00637     d->forwardStack.top().hpos = d->hbar->value();
00638     d->forwardStack.top().vpos = d->vbar->value();
00639     d->setSource(d->stack.top().url);
00640     d->hbar->setValue(d->stack.top().hpos);
00641     d->vbar->setValue(d->stack.top().vpos);
00642     emit backwardAvailable(d->stack.count() > 1);
00643     emit forwardAvailable(true);
00644 }

void QTextBrowser::forward (  )  [virtual, slot]

Changes the document displayed to the next document in the list of documents built by navigating links. Does nothing if there is no next document.

See also:
backward(), forwardAvailable()

Definition at line 653 of file qtextbrowser.cpp.

References backwardAvailable(), d, emit, forwardAvailable(), and setSource().

Referenced by TabbedBrowser::forward(), and keyPressEvent().

00654 {
00655     Q_D(QTextBrowser);
00656     if (d->forwardStack.isEmpty())
00657         return;
00658     if (!d->stack.isEmpty()) {
00659         d->stack.top().hpos = d->hbar->value();
00660         d->stack.top().vpos = d->vbar->value();
00661     }
00662     d->stack.push(d->forwardStack.pop());
00663     setSource(d->stack.top().url);
00664     d->hbar->setValue(d->stack.top().hpos);
00665     d->vbar->setValue(d->stack.top().vpos);
00666     emit backwardAvailable(true);
00667     emit forwardAvailable(!d->forwardStack.isEmpty());
00668 }

void QTextBrowser::home (  )  [virtual, slot]

Changes the document displayed to be the first document the browser displayed.

Definition at line 674 of file qtextbrowser.cpp.

References d, and setSource().

Referenced by TabbedBrowser::home(), and keyPressEvent().

00675 {
00676     Q_D(QTextBrowser);
00677     if (d->home.isValid())
00678         setSource(d->home);
00679 }

void QTextBrowser::reload (  )  [virtual, slot]

Reloads the current set source.

Definition at line 524 of file qtextbrowser.cpp.

References d, s, and setSource().

Referenced by TabbedBrowser::reload(), and MainWindow::showSearchLink().

00525 {
00526     Q_D(QTextBrowser);
00527     QUrl s = d->currentURL;
00528     d->currentURL = QUrl();
00529     setSource(s);
00530 }

void QTextBrowser::backwardAvailable ( bool  available  )  [signal]

This signal is emitted when the availability of backward() changes. available is false when the user is at home(); otherwise it is true.

Referenced by backward(), clearHistory(), forward(), and setSource().

void QTextBrowser::forwardAvailable ( bool  available  )  [signal]

This signal is emitted when the availability of forward() changes. available is true after the user navigates backward() and false when the user navigates or goes forward().

Referenced by backward(), clearHistory(), forward(), and setSource().

void QTextBrowser::sourceChanged ( const QUrl src  )  [signal]

This signal is emitted when the source has changed, src being the new source.

Source changes happen both programmatically when calling setSource(), forward(), backword() or home() or when the user clicks on links or presses the equivalent key sequences.

void QTextBrowser::highlighted ( const QUrl link  )  [signal]

This signal is emitted when the user has selected but not activated an anchor in the document. The URL referred to by the anchor is passed in link.

void QTextBrowser::highlighted ( const QString link  )  [signal]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Convenience signal that allows connecting to a slot that takes just a QString, like for example QStatusBar's message().

void QTextBrowser::anchorClicked ( const QUrl link  )  [signal]

This signal is emitted when the user clicks an anchor. The URL referred to by the anchor is passed in link.

Note that the browser will automatically handle navigation to the location specified by link unless you call setSource() in a slot connected. This mechanism is used to override the default navigation features of the browser.

bool QTextBrowser::event ( QEvent e  )  [protected, virtual]

Reimplemented from QTextEdit.

Definition at line 917 of file qtextbrowser.cpp.

References QTextEdit::event().

00918 {
00919     return QTextEdit::event(e);
00920 }

Here is the call graph for this function:

void QTextBrowser::keyPressEvent ( QKeyEvent ev  )  [protected, virtual]

The event ev is used to provide the following keyboard shortcuts: Keypress Action Alt+Left Arrow backward() Alt+Right Arrow forward() Alt+Up Arrow home()

Reimplemented from QTextEdit.

Definition at line 690 of file qtextbrowser.cpp.

References QEvent::accept(), Qt::AltModifier, backward(), d, forward(), home(), QEvent::ignore(), QKeyEvent::key(), Qt::Key_Back, Qt::Key_Down, Qt::Key_Left, Qt::Key_Right, Qt::Key_Select, Qt::Key_Up, QTextEdit::keyPressEvent(), and QKeyEvent::modifiers().

Referenced by HelpWindow::keyPressEvent().

00691 {
00692 #ifdef QT_KEYPAD_NAVIGATION
00693     switch (ev->key()) {
00694     case Qt::Key_Select:
00695         if (QApplication::keypadNavigationEnabled() && !hasEditFocus()) {
00696             setEditFocus(true);
00697             return;
00698         }
00699         break;
00700     case Qt::Key_Back:
00701         if (QApplication::keypadNavigationEnabled()) {
00702             if (hasEditFocus()) {
00703                 setEditFocus(false);
00704                 ev->accept();
00705                 return;
00706             }
00707         }
00708         QTextEdit::keyPressEvent(ev);
00709         return;
00710     default:
00711         if (QApplication::keypadNavigationEnabled() && !hasEditFocus()) {
00712             ev->ignore();
00713             return;
00714         }
00715     }
00716 #endif
00717 
00718     if (ev->modifiers() & Qt::AltModifier) {
00719         switch (ev->key()) {
00720         case Qt::Key_Right:
00721             forward();
00722             ev->accept();
00723             return;
00724         case Qt::Key_Left:
00725             backward();
00726             ev->accept();
00727             return;
00728         case Qt::Key_Up:
00729             home();
00730             ev->accept();
00731             return;
00732         }
00733     }
00734 #ifdef QT_KEYPAD_NAVIGATION
00735     else {
00736         Q_D(QTextBrowser);
00737         if (ev->key() == Qt::Key_Up) {
00738             d->keypadMove(false);
00739             return;
00740         } else if (ev->key() == Qt::Key_Down) {
00741             d->keypadMove(true);
00742             return;
00743         }
00744     }
00745 #endif
00746     QTextEdit::keyPressEvent(ev);
00747 }

Here is the call graph for this function:

void QTextBrowser::mouseMoveEvent ( QMouseEvent e  )  [protected, virtual]

Reimplemented from QTextEdit.

Definition at line 752 of file qtextbrowser.cpp.

References QTextEdit::mouseMoveEvent().

00753 {
00754     QTextEdit::mouseMoveEvent(e);
00755 }

Here is the call graph for this function:

void QTextBrowser::mousePressEvent ( QMouseEvent e  )  [protected, virtual]

Reimplemented from QTextEdit.

Definition at line 760 of file qtextbrowser.cpp.

References QTextEdit::mousePressEvent().

Referenced by HelpWindow::mousePressEvent().

00761 {
00762     QTextEdit::mousePressEvent(e);
00763 }

Here is the call graph for this function:

void QTextBrowser::mouseReleaseEvent ( QMouseEvent e  )  [protected, virtual]

Reimplemented from QTextEdit.

Definition at line 768 of file qtextbrowser.cpp.

References QTextEdit::mouseReleaseEvent().

Referenced by HelpWindow::mouseReleaseEvent().

00769 {
00770     QTextEdit::mouseReleaseEvent(e);
00771 }

Here is the call graph for this function:

void QTextBrowser::focusOutEvent ( QFocusEvent ev  )  [protected, virtual]

Reimplemented from QTextEdit.

Definition at line 776 of file qtextbrowser.cpp.

References d, QTextEdit::focusOutEvent(), Qt::IBeamCursor, and Qt::TextEditable.

00777 {
00778     Q_D(QTextBrowser);
00779 #ifndef QT_NO_CURSOR
00780     d->viewport->setCursor((!(d->control->textInteractionFlags() & Qt::TextEditable)) ? d->oldCursor : Qt::IBeamCursor);
00781 #endif
00782     QTextEdit::focusOutEvent(ev);
00783 }

Here is the call graph for this function:

bool QTextBrowser::focusNextPrevChild ( bool  next  )  [protected, virtual]

Reimplemented from QTextEdit.

Definition at line 788 of file qtextbrowser.cpp.

References d, and QTextEdit::focusNextPrevChild().

00789 {
00790     Q_D(QTextBrowser);
00791     if (d->control->setFocusToNextOrPreviousAnchor(next))
00792         return true;
00793     return QTextEdit::focusNextPrevChild(next);
00794 }

Here is the call graph for this function:

void QTextBrowser::paintEvent ( QPaintEvent e  )  [protected, virtual]

Reimplemented from QTextEdit.

Definition at line 799 of file qtextbrowser.cpp.

References d, and p.

00800 {
00801     Q_D(QTextBrowser);
00802     QPainter p(d->viewport);
00803     d->paint(&p, e);
00804 }

QTextBrowser::Q_PRIVATE_SLOT ( d_func()  ,
void   _q_documentModified() 
) [private]

Reimplemented from QWidget.

void QTextBrowser::_q_activateAnchor ( const QString  )  [private]


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 19:21:34 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1