6

I would like to add two widgets (say QPushButton) to the status bar, one to the left and other to the right side.

I am thinking of adding horizontal spacer in between the two widgets, but don't know how to add.

PS: I tried using addWidget() to add to the left and addPermanentWidget() to add to the right but it doesn't look neat and also it doesn't feel right.

azalea
  • 11,402
  • 3
  • 35
  • 46
user2653062
  • 697
  • 1
  • 9
  • 16
  • 1
    What exactly is your requirement ? Please explain it with more clarity. (examples maybe helpful) – Pratham Sep 09 '14 at 14:01
  • I want to add two push buttons on the status bar, one to the left side and other to the right side such that they have identical spacing from the respective left and right borders. In other words, one button should be left aligned and other should be right aligned. – user2653062 Sep 09 '14 at 14:10

3 Answers3

10

You can add two buttons to a layout in a widget and add the widget to the status bar using QStatusBar::addWidget :

QWidget * widget = new QWidget();
QPushButton  * leftBut = new QPushButton("Left");
QPushButton  * rightBut = new QPushButton("Right");
QGridLayout * layout = new QGridLayout(widget);
layout->addWidget(leftBut,0,0,1,1,Qt::AlignVCenter | Qt::AlignLeft);
layout->addWidget(rightBut,0,1,1,1,Qt::AlignVCenter | Qt::AlignRight);
ui->statusBar->addWidget(widget,1);
Nejat
  • 31,784
  • 12
  • 106
  • 138
  • Where will the messages appear after setting this layout ? Will it still work ? – Overdrivr Jan 09 '18 at 10:45
  • @Overdrivr You can use a `QLabel` instead of `QWidget` and make it transparent by someway like this: https://stackoverflow.com/questions/23948453/c-over-qt-controlling-transparency-of-labels-and-buttons/23948901#23948901 – Nejat Jan 09 '18 at 10:50
6

I am thinking of adding horizontal spacer in between the two widgets, but don't know how to add.

Here is a way to use a "fake" spacer.

QPushButton *leftButton = new QPushButton("Left");
QPushButton *rightButton = new QPushButton("Right");
QLabel *spacer = new QLabel(); // fake spacer
ui->statusBar->addPermanentWidget(leftButton);
ui->statusBar->addPermanentWidget(spacer, 1);
ui->statusBar->addPermanentWidget(rightButton);

The second parameter in addPermanentWidget is "used to compute a suitable size for the given widget as the status bar grows and shrinks".

Demo:

Result looks like this.

azalea
  • 11,402
  • 3
  • 35
  • 46
1

I think the simplest way is using a QGridLayout (honestly I never tried to modify a status bar anyway) supposing that the status bar is or descends from widget you can do this:

QGridLayout *myGridLayout = new QGridLayout();
statusbar->setLayout(myGridLayout)

QPushButton *button1 = new QPushButton(this);
myGridLayout->addWidget(button1,0,0,1,1);

QPushButton *button2 = new QPushButton(this);
myGridLayout->addWidget(button2,X,0,1,1);

The biggest is X the more space you want to leave in between, I would suggest to start with 3 and then make few tests to see how it looks.

Morix Dev
  • 2,700
  • 1
  • 28
  • 49
Marco
  • 1,952
  • 1
  • 17
  • 23