I create a messagebar using this

iface.messageBar().pushWidget(iface.messageBar().createMessage( 'Hello World' ), Qgis.Info)

How can I add, say, 5 spaces between Hello and World?

I could use '<pre>Hello World</pre>' to output exactly what I type but this changes the font of the messagebar. I would like to retain the same default font.

Is there another way?


Qgis interface Message bar convert text into HTML for display. Multiples spaces are considered as only one in HTML, unless they are non-breaking spaces &nbsp;.

So, you can achieve your five blank spaces text with :

iface.messageBar().pushMessage("", f"Hello{'&nbsp;' * 5}World", Qgis.Info, -1)


'&nbsp;' * 5 gives '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

f"my string with curly brackets" it's the new format way since Python 3.6

So with f"Hello{'&nbsp;' * 5}World", I create this string "Hello&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;World" or Hello     World.

See :

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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