QTextBrowser::highlighted() signal not being sent when TAB key is used to navigate links?
-
I have been successfully able to add a status bar to a dialog which displays different QTextBrowser widgets on each page of a tab widget. There are various external links (URLs) in the pages. When I hover over a link with the mouse, the text of the URL is displayed in the status bar just like in most browsers.
However, I noticed that I can navigate the links by using the TAB key. It seems like the link is "highlighted" when I focus on a link, because a focus rectangle is drawn around the link by the text browser. I have connected a slot for each browser widget to the
highlighted(const QUrl &)signal emitted by the text browser, wishing to display the link in the status bar, too, but nothing happens.My current setup (for one browser widget; there are nine of them) is as follows:
I have installed an event filter on each browser widget to catch theHoverEvent; each widget also has the window attributeWA_Hoverset. Here is my code for the event filter (this works OK):bool DlgAbout::eventFilter(QObject *obj, QEvent *event) { QTextBrowser *browser = qobject_cast<QTextBrowser*>(obj); if (browser && (event->type() == QEvent::HoverEnter || event->type() == QEvent::HoverMove || event->type() == QEvent::HoverLeave)) { QHoverEvent * pHover = static_cast<QHoverEvent*>(event); if (event->type() == QEvent::HoverEnter || event->type() == QEvent::HoverMove) { QPoint pt = pHover->pos(); QString anchor = browser->anchorAt(pt); if (!anchor.isEmpty()) { // sb_ is a pointer to the status bar, a member variable: sb_->showMessage(anchor); } else { sb_->clearMessage(); } return true; } else if (event->type() == QEvent::HoverLeave) { sb_->clearMessage(); return true; } } return QObject::eventFilter(obj, event); }I have slots like this one to catch the
highlighted()signal:void DlgAbout::on_txtQt_highlighted(const QUrl &url) { sb_->showMessage(url.toDisplayString()); }And here is the connection syntax:
connect(ui->txtQt, QOverload<const QUrl &>::of(&QTextBrowser::highlighted), this, &DlgAbout::on_txtQt_highlighted);The overload seems still to be necessary, although the version taking a
const QString &has apparently been marked as deprecated since Qt 5.15 (I am using Qt 5.15.5).Any help would be greatly appreciated, as usual.
-
Is there some other signal I should be connecting to, or a property of the underlying
QTextDocumentthat would help retrieve the URL which is currently focused?