Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTextBrowser::highlighted() signal not being sent when TAB key is used to navigate links?
Forum Updated to NodeBB v4.3 + New Features

QTextBrowser::highlighted() signal not being sent when TAB key is used to navigate links?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 1 Posters 217 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Robert Hairgrove
    wrote on last edited by
    #1

    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 the HoverEvent; each widget also has the window attribute WA_Hover set. 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.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Robert Hairgrove
      wrote on last edited by
      #2

      Is there some other signal I should be connecting to, or a property of the underlying QTextDocument that would help retrieve the URL which is currently focused?

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved