4

I have a QMessageBox and want to create a link on a new line. I cannot get the link to work when I use \n

link = "www.google.com"
msg = "This works: <a href='%s'>Google</a>" % link
reply = QMessageBox.information(self, 'Message', msg, QMessageBox.Ok, QMessageBox.Ok)

msg = "This does not work: \n<a href='%s'>Google</a>" % link
reply = QMessageBox.information(self, 'Message', msg, QMessageBox.Ok, QMessageBox.Ok)

I would like to get the second example to work.

user1408329
  • 53
  • 1
  • 6

1 Answers1

4

If you use HTML formatting the newline character has no special meaning - it's just whitespace as anything else, and is collapsed to a single space actually, it does seem to throw off the HTML auto-detector, which falls back to plain text. Anyhow, to put your link on a separate line use the <br> line break tag, or put the two lines in different paragraphs using the <p> tag.

msg = "This works as well:<br><a href='%s'>Google</a>" % link
reply = QMessageBox.information(self, 'Message', msg, QMessageBox.Ok, QMessageBox.Ok)

demo dialog with newline working

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299