QHBoxLayout inside QFormLayout not left aligned - qt

I have following chunk of simple code:
UeDisplaySettingsWidget::UeDisplaySettingsWidget(QWidget *parent)
: QWidget(parent)
{
this->setLayout(new QFormLayout());
this->ueSetDisplayDimmerSelectorLayout(new QHBoxLayout());
this->ueSetDisplayDimmerSelectorLabel(new QLabel(tr("Display fade level:"),
this));
this->ueInitDisplayDimmerLevelSelector();
this->ueInitDisplayDimmerTimeoutSelector();
connect(this->ueDisplayDimmerSelector(),
&QSlider::valueChanged,
this,
&UeDisplaySettingsWidget::ueSlotDisplayDimerSelectorValueChanged);
this->ueDisplayDimmerSelectorLayout()->addWidget(this->ueDisplayDimmerSelectorLabel());
this->ueDisplayDimmerSelectorLayout()->addWidget(this->ueDisplayDimmerSelector());
this->ueDisplayDimmerSelectorLayout()->addWidget(this->ueDisplayDimmerCurrentLevelIndicator());
dynamic_cast<QFormLayout*>(this->layout())->addItem(this->ueDisplayDimmerSelectorLayout());
dynamic_cast<QFormLayout*>(this->layout())->addRow(tr("Display fade timeout:"),
this->ueDisplayDimmerTimeoutSelector());
} // constructor
which produces following output:
Now, why QHBoxLayout returned by this->ueDisplayDimmerSelectorLayout() and containing QLabel with text Display fade level:, QSlider and QLCDNumber is not aligned with lower row of QFormLayout?

QFormLayout is basically a grid layout with two columns where you have labels in the left column and whatever in the right.
For some reason you but that fade out label this->ueDisplayDimmerSelectorLabel() in the horizontal layout and use QFormLayout::addItem(QLayoutItem*) for your first row and QFormLayout::addRow(const QString &, QWidget*)) for the second row. Thats why your DisplayDimmerSlectorLabel is together with its widget in the second column. Wtihout having it tested it should look something like this:
connect(this->ueDisplayDimmerSelector(),
&QSlider::valueChanged,
this,
&UeDisplaySettingsWidget::ueSlotDisplayDimerSelectorValueChanged);
this->ueDisplayDimmerSelectorLayout()->addWidget(this->ueDisplayDimmerSelector());
this->ueDisplayDimmerSelectorLayout()->addWidget(this->ueDisplayDimmerCurrentLevelIndicator());
dynamic_cast<QFormLayout*>(this->layout())->addRow(tr("Display fade level:"), this->ueDisplayDimmerSelectorLayout());
dynamic_cast<QFormLayout*>(this->layout())->addRow(tr("Display fade timeout:"),
this->ueDisplayDimmerTimeoutSelector());

Related

How to set a center aligned QLabel to toolbar in Qt

I'm new to qt and exploring it .I want to have a text which is center aligned in my Mainwindow's toolbar.Below is my code inside my MainWindow constructor:
QLabel* label=new QLabel("Hello World");
label->setAlignment(Qt::AlignHCenter);
QHBoxLayout *layout = new QHBoxLayout();
layout->addWidget(label);
QWidget* wid = new QWidget;
wid->setLayout(layout);
ui->mainToolBar->addWidget(wid);
The above code displays the text , but not at the center.It displays at the left.What am I missing?Any help will be really helpful.
label->setAlignment(Qt::AlignHCenter);
This tells the label to (horizontally) center the text in itself.
layout->addWidget(label);
This is expanded by default argument to
layout->addWidget(label, 0);
Where the 0 is the stretch factor of the label within this layout. Zero means your label will be given as much space as it needs to display properly but nothing more. So your label is just as big as your text needs, has it's text centered, but since it's on a QHBoxLayout it's shown on the left side within your bar. If there are no other widgets in your bar's layout, you can set the stretch-factor to 1 to make the label fill the layout, your text will then show in the center.
layout->addWidget(label, 1);

Allignment of a QLabel and a QCheckBox in a BoxLayout gives unexpected result

when i add a QLabel and QCheckBoxs to either a QVBoxLayout or a QHBoxLayout i would expect them to be evenly distributed but the checkboxes will allign tight at the very bottom (in the above example) and the label will be centered on the resulting free space in the widget. How can i change this behaviour to distribute all 3 widgets evenly?
Many thanks.
This is the example code:
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
QLabel* l = new QLabel("Hi");
QCheckBox* c = new QCheckBox("Label");
QCheckBox* c2 = new QCheckBox("Label");
l->setText("Hi");
QVBoxLayout* v = new QVBoxLayout;
v->addWidget(l);
v->addWidget(c);
v->addWidget(c2);
setLayout(v);
ui->setupUi(this);
}
And this is the result:
Take a look at QSizePolicy. You need to setSizePolicy for your QLabel and QCheckBoxes to be QSizePolicy::Preferred, from the docs:
The default policy is Preferred/Preferred, which means that the widget
can be freely resized, but prefers to be the size sizeHint() returns.
Button-like widgets set the size policy to specify that they may
stretch horizontally, but are fixed vertically. The same applies to
lineedit controls (such as QLineEdit, QSpinBox or an editable
QComboBox) and other horizontally orientated widgets (such as
QProgressBar).
Currently, your QLabel has preferred height, while both of the QCheckBoxes has fixed height. That means that the QLabel will be expanded automatically to take up any additional vertical space (that can't be taken by the QCheckBoxes.
So, In order to set all your widgets to Preferred height, you need to add the following to your constructor:
l->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
c->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
c2->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
Another option, would be to add spacers around each one of the widgets, like this:
v->addStretch();
v->addWidget(l);
v->addStretch();
v->addWidget(c);
v->addStretch();
v->addWidget(c2);
v->addStretch();
setLayout(v);
This way QSpacerItems will take up all the additional space.

QGridLayout Widget disappearing after using QAlignment

I have the following:
QGridLayout *layout = new GridLayout();
layout->addWidget(rightBuf, row, 0);
layout->addWidget(i, row, 1);
layout->addWidget(leftBuf, row, 2);
Which displays fiine. However, when I add the alignment to the second line, ie:
layout->addWidget(i, row, 1, Qt::AlignTop);
The widget disappears from the screen. I think I have tried about just everything to get it to align top, including putting it into a QVBoxLayout and then adding it to the QGridLayout. Is there anything missing here?

Create Qt layout with fixed height

I want to create a Qt window that contains two layouts, one fixed height that contains a list of buttons at the top and one that fills the remaning space with a layout that centers a widget vertically and horizontally as per the image below.
How should i be laying out my layouts/widgets. Ive tried a few options with nested horizontal and vertical layouts to no avail
Try making the pink box a QWidget with a QHBoxLayout (rather than just making it a layout). The reason is that QLayouts don't provide functionality to make fixed sizes, but QWidgets do.
// first create the four widgets at the top left,
// and use QWidget::setFixedWidth() on each of them.
// then set up the top widget (composed of the four smaller widgets):
QWidget *topWidget = new QWidget;
QHBoxLayout *topWidgetLayout = new QHBoxLayout(topWidget);
topWidgetLayout->addWidget(widget1);
topWidgetLayout->addWidget(widget2);
topWidgetLayout->addWidget(widget3);
topWidgetLayout->addWidget(widget4);
topWidgetLayout->addStretch(1); // add the stretch
topWidget->setFixedHeight(50);
// now put the bottom (centered) widget into its own QHBoxLayout
QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->addStretch(1);
hLayout->addWidget(bottomWidget);
hLayout->addStretch(1);
bottomWidget->setFixedSize(QSize(50, 50));
// now use a QVBoxLayout to lay everything out
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(topWidget);
mainLayout->addStretch(1);
mainLayout->addLayout(hLayout);
mainLayout->addStretch(1);
If you really want to have two separate layouts--one for the pink box and one for the blue box--the idea is basically the same except you'd make the blue box into its own QVBoxLayout, and then use:
mainLayout->addWidget(topWidget);
mainLayout->addLayout(bottomLayout);

Reversing the layout of a QToolButton

I would like to make a QToolButton where the text is the left of its icon. I've tried to find related information, and tried things with:
button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
but this sets the text to the right side of the button. I've looked into trying stylesheets, but text-alignment is only for push buttons. Is there any way to do this?
My highest-level goal is to make an interface bar which looks like this:
with a text label and image right beside it. Currently I'm using a toolbar with checkable toolbuttons because of the style (no border unless moused-over, still has the indent with checkable, contains text and an icon...). It's entirely possible that I'm using the wrong type widgets, so if I can't change the layout of this, is there any way to emulate this style?
This is what I currently have:
.
You can use QWidget::setLayoutDirection:
toolbar->setLayoutDirection(Qt::RightToLeft);
Your best bet is probably to create this functionality yourself in a QWidget. It should have a QLabel and a QToolButton (or QPushButton) arranged in a QHBoxLayout. Then you can add this custom widget to the toolbar using QToolBar::addWidget() or QToolBar::insertWidget().
Here's an example of a widget you could use:
#include <QtGui>
class CoolButton : public QWidget
{
public:
CoolButton(QWidget *parent = 0) : QWidget(parent)
{
QLabel *label = new QLabel("Hi!", this);
QToolButton *button = new QToolButton(this);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(label);
layout->addWidget(button);
setLayout(layout);
}
};

Resources