Handling Hyperlinks in Qt's 'What's This?' Help Text


  • Context
    "What's This?" text is a common feature in Qt applications. It typically appears as a tooltip-like message that provides contextual help when the user hovers over a specific element. Some applications allow embedding hyperlinks within this help text, and QWhatsThisClickedEvent helps you handle clicks on those hyperlinks.

  • Functionality
    When a user clicks on such a hyperlink, a QWhatsThisClickedEvent object is generated by Qt. This event object encapsulates information about the click, allowing your program to react accordingly.

  • Purpose
    It's an event class specifically designed to catch clicks on hyperlinks displayed as "What's This?" help text.

By overriding the event() handler in your Qt widget class, you can listen for QWhatsThisClickedEvent objects. This allows you to execute custom logic whenever a user clicks on a hyperlink in the "What's This?" text. For instance, you might open a web page related to the clicked hyperlink or display additional information within the application itself.



#include <QApplication>
#include <QLabel>
#include <QWhatsThisClickedEvent>

class MyWidget : public QLabel {
  Q_OBJECT

public:
  explicit MyWidget(QWidget *parent = nullptr) : QLabel(parent) {
    setText("Click <a href=\"some_link\">here</a> for more info.");
    installEventFilter(this); // Install event filter on this widget itself
  }

protected:
  bool eventFilter(QObject *obj, QEvent *event) override {
    if (event->type() == QEvent::WhatsThisClicked) {
      auto whatsThisEvent = static_cast<QWhatsThisClickedEvent*>(event);
      const QString& clickedLink = whatsThisEvent->href();

      // Handle the clicked link (e.g., open a web page)
      if (clickedLink == "some_link") {
        // Simulate opening a web page (replace with your desired action)
        qDebug() << "Opening link: " << clickedLink;
      }

      return true; // Mark event as handled
    }

    return QObject::eventFilter(obj, event);
  }
};

int main(int argc, char *argv[]) {
  QApplication app(argc, argv);
  MyWidget widget;
  widget.show();
  return app.exec();
}
  1. We define a custom widget class MyWidget that inherits from QLabel.
  2. In the constructor, we set the label text with a hyperlink using HTML anchor tag (<a>) and a custom URL (some_link).
  3. We install an event filter on the widget itself using installEventFilter(this). This allows us to intercept events before they reach the widget.
  4. The eventFilter function is overridden to handle incoming events.
  5. We check if the event type is QEvent::WhatsThisClicked. If it is, we cast the event to QWhatsThisClickedEvent to access the clicked link using the href() method.
  6. Inside the if block, we check if the clicked link matches our expected URL ("some_link").
  7. If the link matches, we perform an action (here, simulating opening a web page using qDebug). You can replace this with your desired behavior, such as opening a real web page or displaying additional information within the application.
  8. Finally, we return true from the eventFilter function to indicate that the event has been handled.


  1. Custom Tooltips with Signals and Slots
  • Connect signals emitted from these custom elements within the tooltip to slots in your main widget to handle user interaction.
  • Implement custom widgets or buttons within the tooltip using Qt's layout system.
  • Use Qt's QToolTip class to manage the tooltip itself.
  • Instead of relying on hyperlinks within "What's This?" text, you can create custom tooltips that display rich text with clickable elements (like buttons or labels).
  1. Separate Help System
  • This system could be a context-sensitive help window or a dedicated documentation section within your application.
  • If the information you want to provide with the hyperlink goes beyond a simple link, consider creating a separate help system.
  1. Rich Text Formatting with Callbacks
  • This approach allows some level of interactivity within the label itself, but it's less flexible compared to hyperlinks.
  • You can embed specific keywords or phrases within the rich text that trigger custom callbacks when clicked.
  • Qt supports rich text formatting within labels and other widgets using HTML-like syntax.

The best alternative depends on the complexity of the information you want to provide and the desired level of user interaction.

Here are some additional points to consider:

  • User Experience
    Well-designed custom tooltips can provide a seamless user experience, while a separate help system might require users to switch context.
  • Maintainability
    A separate help system can be more maintainable for extensive documentation but might require additional development effort.
  • Complexity
    Creating custom tooltips with clickable elements might be more complex than using QWhatsThisClickedEvent.