5

How can I center align the text in QStatusBar? By default it is always left aligned.

I know I can add a QLabel and set alignment, but I want to use plain text, and .showMessage(QString, int) method, so I can add the timeout value.

Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
ZolaKt
  • 4,683
  • 8
  • 43
  • 66

2 Answers2

8

A QStatusBar has three functions of note here:

addPermanentWidget - Places a widget right aligned

addWidget - Places a widget left aligned which can be obscured by status messages

showMessage - Displays a status message

These are well established standards for status bars. While you could hack away to get what you're looking for, I'd suggest you reconsider your needs. Perhaps your QLabel should be placed with addPermanentWidget instead?

Take a look at the docs for more info: http://doc.qt.io/qt-5/qstatusbar.html

Christophe Weis
  • 2,518
  • 4
  • 28
  • 32
OliJG
  • 2,650
  • 1
  • 16
  • 15
  • ok. but how can I add the timeout if I use addPermanenetWidget? – ZolaKt Dec 10 '10 at 11:03
  • 1
    With QTimer. start it with singleshot when display message on qlabel, and create simply function which cleans label text on timeout. – Raiv Dec 10 '10 at 15:46
  • @ZolaKt: What do you mean? Are you trying to display the temporary text in a QLabel? Why not just use the showMessage function as it was intended? – OliJG Dec 11 '10 at 01:10
  • Yes I'm trying to display the temporary text. I am using the showMessage function. I'd just like the text to be center aligned, instead of default left – ZolaKt Dec 12 '10 at 13:36
  • If your concern is that it overwrites the other widgets, then those widgets should probably be placed with addPermanentWidget. If you just want it in the middle for the sake of it... ugh. And don't expect to use showMessage. – OliJG Dec 13 '10 at 02:22
6

If you just want to center the message itself in the whole statusBar, go like this:

QLabel* statusLabel = new QLabel("Your Message");
statusBar()->addWidget(statusLabel,1);

This additional parameter 1 stretches your Label to the complete width of the statusbar.

Greetings

Tschouns
  • 59
  • 1
  • 1