Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: How to add two widgets (say QPushButton) to the status bar, one to the left and other to the right side?

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.

like image 337
user2653062 Avatar asked Sep 09 '14 13:09

user2653062


People also ask

How do I add a horizontal spacer in Qt?

You can add a spacer item, either on the outside of your horizontal layout, or inside your horizontal layout. If you add the spacer item inside the horizontal layout, you have to make sure you add your label widgets before the spacer item. Use the insertWidget() function to add your labels in this case.

How do I add widgets to Qt?

To add widgets in Qt Designer: In the Qt Creator Edit mode, double-click the notepad. ui file in the Projects view to launch the file in the integrated Qt Designer. Drag and drop widgets Text Edit (QTextEdit) to the form.

What is QWidget * parent?

The tree-like organization of QWidgets (and in fact of any QObjects ) is part of the memory management strategy used within the Qt-Framework. Assigning a QObject to a parent means that ownership of the child object is transferred to the parent object. If the parent is deleted, all its children are also deleted.


1 Answers

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);
like image 81
Nejat Avatar answered Sep 21 '22 19:09

Nejat