How to make a QGraphicsTextItem clickable?

2019-08-03 12:44发布

问题:

In the "About box" of my software, I used a QGraphicsTextItem to show the about-text.

This text contains hypertext links (in the form of: <a href="http://some.random.site">link</a>).

The item shows up properly (hypertext links are blue and underlined). However, when I click on them, nothing happens.

Here is how I created the QGraphicsTextItem:

d_about_text_item = new QGraphicsTextItem;
d_about_text_item->setTextInteractionFlags(Qt::TextBrowserInteraction);
d_about_text_item->setHtml(aboutText());

As I understand the Qt documentation, the call to setTextInteractionFlags should allow me to handle special hypertext links click events.

Is there anything else I should do to be able to click on the links and show up the linked page in the default system browser ?

回答1:

I found what I did wrong:

My containing QGraphicsView had setInteractive() set to false. I removed it and since now, it works fine.



回答2:

FWIW I use the standard QMessageBox::about method and simply pass raw HTML as the text - links work fine.

QMessageBox::about(this, tr("About"), tr("<h1>My App</h1><p><a href='www.stackoverflow.com'>Click me!</a></p>"));