0

Follow-up from this question: Including hyperlink in QMessageBar

Is it possible to stop the QMessageBar from closing when the hyperlink is clicked?

A simple messagebar snippet I am using:

iface.messageBar().pushMessage("", '<a href="https://www.w3schools.com/">Visit W3Schools.com!</a>', Qgis.Info, 0)

1 Answer 1

3

Instead of displaying a hyperlink in the QgsMessageBar, I use the method pushWidget to display a QLabel that has a hyperlink, so when you click, the message bar won't close.

Here's the code :

# create the Qt Label
link_label = QLabel(
    '<a href="https://gis.stackexchange.com/a/438269/93097">'
    "Visit and vote for this awesome answer 😎!</a>"
)
# it can now open your favorite internet browser
link_label.setOpenExternalLinks(True)
# display the iface messagebar with the label
iface.messageBar().pushWidget(link_label)

Documentation

1
  • This works great! Also nice touch to your answer :-)
    – jim
    Aug 16, 2022 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.