PDA

View Full Version : Adding widget to bottom left corned of existing Widget



carobnodrvo
15th July 2016, 17:01
Hi,

currently I am trying to achieve something like this:
12034

So red widget is centralWidget of my QMainWindow and now I want to add another small widget (yellow one) inside it and put it in bottom left corner.

If I do something like this


QHBoxLayout* layout = new QHBoxLayout(this);


QWidget* red = new QWidget(this);
red->setStyleSheet("background:red;");
red->setMaximumSize(400, 400);

layout->addWidget(red);

MyCustomWidget* mainWidget = new MyCustomWidget(this);
mainWidget->setLayout(layout);

setCentralWidget(mainWidget);

I see my widget but it's centered vertically and horizontally inside my MyCustomWidget.
If I try changing "addWidget" line to :


layout->addWidget(red, 0, Qt::AlignLeft | Qt::AlignBottom);

I don't see my widget anymore.

Could someone tell me why is it happening and how can I fix it?
Is my approach good?

anda_skoa
16th July 2016, 12:32
If your red widget does not have any other children than the yellow widget, then the easiest way is to use a QGridLayout, with two rows and two columns.

The child goes into second row/first colum, a vertical spacer into first row/first column and a horizontal spacer into second row/second column.

Btw, your code snippet is misleading, because you make your "yellow widget" red.

Cheers,
_

carobnodrvo
18th July 2016, 09:19
Ups, you are right (red widget is actually yellow :/)

At the end I ended up doing it so.

Thanks!