Alternatives to QGraphicsTextItem::linkHovered() for Link Hovering


  1. Create a QTextDocument
    This will hold the formatted text content that may contain links.

  2. Set Text Interaction Flags
    Use the setTextInteractionFlags method on the QGraphicsTextItem to enable text interaction flags like Qt::TextBrowserInteraction. This allows the text item to respond to user clicks and hover events.

  3. Handle Hover Events
    In your application's event loop, listen for the hoverEnterEvent and hoverLeaveEvent signals emitted by the QGraphicsTextItem.

  4. Identify Hovered Text
    Within the event handlers, use the cursorForPosition(event->pos()) method to get the text fragment at the cursor position.

  5. Simulate Link Hovering
    Here, you can implement your own logic to identify if the hovered text corresponds to a link. You can maintain a separate data structure (like a map) that associates text fragments with URLs.

  6. Update UI (Optional)
    Based on the hovered text, you can change the cursor appearance or display a tooltip using Qt's tooltip functionality to indicate it's a link.



#include <QGraphicsTextItem>
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
#include <QtTextDocument>
#else
#include <QtGui/QTextDocument>
#endif
#include <QMouseEvent>
#include <QUrl>

class ClickableTextItem : public QGraphicsTextItem {
  Q_OBJECT

public:
  ClickableTextItem(const QString& text, QTextDocument* document);

protected:
  void hoverEnterEvent(QHoverEvent* event) override;
  void hoverLeaveEvent(QHoverEvent* event) override;

private:
  QUrl m_linkUrl;  // Stores the URL for the hovered link (if any)
  QTextDocument* m_textDocument;
};

ClickableTextItem::ClickableTextItem(const QString& text, QTextDocument* document)
  : QGraphicsTextItem(text), m_textDocument(document), m_linkUrl(QUrl()) {
  setTextInteractionFlags(Qt::TextBrowserInteraction);
}

void ClickableTextItem::hoverEnterEvent(QHoverEvent* event) {
  QTextCursor cursor = m_textDocument->cursorForPosition(event->pos());
  QString text = cursor.selectedText();

  // Simulate link hovering behavior (replace with your link detection logic)
  if (text.startsWith("link:")) {
    m_linkUrl = QUrl(text.mid(5));  // Extract URL after "link:" prefix
    setCursor(Qt::PointingHandCursor);  // Change cursor to indicate a link
  } else {
    m_linkUrl = QUrl();
    setCursor(Qt::ArrowCursor);  // Reset cursor to default
  }
}

void ClickableTextItem::hoverLeaveEvent(QHoverEvent* event) {
  m_linkUrl = QUrl();
  setCursor(Qt::ArrowCursor);  // Reset cursor to default
}

int main(int argc, char* argv[]) {
  QApplication app(argc, argv);

  // Create a text document with a link
  QString text = "This is a text with a link: link:https://www.example.com";
  QTextDocument document;
  document.setHtml(text);

  // Create the clickable text item
  ClickableTextItem* textItem = new ClickableTextItem(document.toPlainText(), &document);
  textItem->setPos(10, 10);
  textItem->show();

  return app.exec();
}

This code creates a ClickableTextItem class that inherits from QGraphicsTextItem. It overrides the hoverEnterEvent and hoverLeaveEvent to handle cursor changes based on whether the hovered text corresponds to a link (identified by the "link:" prefix here).



  1. Custom QTextDocument Subclass
  • Connect this custom signal to a slot in your application logic to handle link hovering behavior (e.g., update cursor, display tooltip).
  • Based on the hovered text being a link, emit a custom signal (e.g., linkHovered(const QUrl&)) from the document subclass.
  • Implement your link detection logic based on the fragment's content or associated data.
  • Within mouseMoveEvent, use cursorForPosition(event->pos()) to identify the hovered text fragment.
  • Create a subclass of QTextDocument that overrides the mouseMoveEvent method.
  1. Rich Text Formatting with Links
  • Qt will handle basic link hovering behavior (changing cursor to pointing hand). You can further customize tooltips or actions using QTextDocument::anchorValue to retrieve link information at the hovered position.
  • Set setTextInteractionFlags on QGraphicsTextItem to Qt::TextBrowserInteraction.
  • Use HTML or Rich Text formatting features within QTextDocument to define actual hyperlinks within your text content.
  1. External Library
  • Consider using libraries like QtWebKit or QtWebEngine that offer more advanced text rendering and link handling capabilities. These libraries can provide a richer experience for handling links within your application.