tags:

views:

178

answers:

1

I want to show the user a warning QMessageBox with a link inside. This is relatively easy, I just need to make sure I set the RichText text format on the message box and the QMessageBox setup does the rest. However, I would also like to close the message box (as in some-sort-of-call-to done()) if the user clicks on the link - the semantic being that the user acknowledged the message and made a decision.

The problem: QMessageBox hides the linkActivated signal coming from its inner QLabel (which is used to store the text).

I thought I could extend the QMessageBox class and do this very ugly hack in the constructor:

QLabel *lbl = findChild<QLabel*>(QString("qt_msgbox_label"));
assert(lbl != NULL);

connect(lbl, SIGNAL(linkActivated(const QString&)), this, SLOT(handle_link_activation(const QString&)));

but although the label found with findChild is not null, and the "qt_msgbox_label" is definitely correct (c/p'ed from the source), and there is no "no such signal/slot" message, my slot never gets called when I click the link.

I'd like to avoid writing my own QDialog which would mimic the QMessageBox behavior. Does anyone have any idea on how I can catch that signal?

+1  A: 

Try defining your own link "protocol" i.e. msgboxurl://yoururl.is.here and install url handler for it

QDesktopServices::setUrlHandler("msgboxurl", urlHandlerObj, "slotName");

urlHandlerObj may be object that created message box. In slot you can just hide your message box and take url part after // and open it with QDesktopServices::openUrl but remember that you have to prepend then http/https prefix (on some platforms url without "scheme" is not handled properly). Slot handling url must have same parameters as QDesktopServices::openUrl static method

Kamil Klimek
Thank you for this - it helped me figure out the big part I was missing (and I quote): `openExternalLinks: Specifies whether QLabel should automatically open links using QDesktopServices::openUrl() instead of emitting the linkActivated() signal.`
laura