Posts Tagged ‘Add new tag’

Qt: QWhatsThis – handling clicks

Saturday, June 13th, 2009

QWhatsThis::showText() shows beautiful hint where one can place HTML formatted text. The question is: how we can handle link clicks, like <a href=”more”>more…</a>?

The answer is located in this thread: one should re-implement QWidget::event() method for “parent” widget (3rd param in QWhatsThis::showText() method), like:

void MyWidget::event(QEvent *_event)
{
    if (_event->type() == QEvent::WhatsThisClicked){
        if (QWhatsThisClickedEvent* clicked = dynamic_cast(_event)){
            QString href = clicked->url(); // here is the "href" value of the link user clicked
            ...........
        }
    }
    return QWidget::event(_event);
}

So, when user clicks on the <a href> link, MyWidget::event() fires, extracts the “url” and handles it.

If you don’t want to re-implement the QWidget::event() method, you could use installEventFilter().