PDA

View Full Version : Insert a layout into ScrollArea's layout



Grzyboo
5th September 2015, 01:34
What I want to achieve is this:
11355

The second TextEdit needs a spacer in a horizontal layout for this. I've made a layout like this:


QHBoxLayout* hLayout = new QHBoxLayout();
hLayout->addItem(new QSpacerItem(16, 64, QSizePolicy::Fixed, QSizePolicy::Fixed));
hLayout->addWidget((QTextEdit*)itemText); //Just fyi: itemText is an object of class that extends QTextEdit.


and tried to add it to an existing (vertical)layout that is set on the ScrollArea.


ui->scrollArea->widget()->layout()->addItem(hLayout);


But it only adds empty space with the height of spacer. Why doesn't the QTextEdit object appear?

cnut
5th September 2015, 08:12
You need to use 2 layouts that are nested.

Take a look at this...


QHBoxLayout* hLayout = new QHBoxLayout;
QVBoxLayout* vLayout = new QVBoxLayout;


QTextEdit* te1 = new QTextEdit("test text", this);
QTextEdit* te2 = new QTextEdit("test text2", this);

QSpacerItem* spacer = new QSpacerItem(16,64, QSizePolicy::Fixed, QSizePolicy::Fixed);

hLayout->addItem(spacer);
hLayout->addWidget(te2);

vLayout->addWidget(te1);
vLayout->addItem(hLayout);

ui->scrollAreaWidgetContents->setLayout(vLayout);


this code's output is what you've asked.

Grzyboo
5th September 2015, 11:01
Yes, it works. But how to add a widget to an existing layout? Without creating a vertical one.

anda_skoa
5th September 2015, 11:39
What kind of layout do you currently have on ui->scrollArea->widget()?

Is your code executed before or after the scroll area has been shown?

Cheers,
_

Grzyboo
5th September 2015, 13:31
After it has been shown. Added the ScrollArea in ui and I want to add multiple widgets into it with code.

anda_skoa
5th September 2015, 14:07
After it has been shown. Added the ScrollArea in ui and I want to add multiple widgets into it with code.

Ok, then make sure you also call show() on the new widgets.

show() of child widgets is only automatic when they have been added before their parent is shown for the first time.
After that they are created in hidden state and need to be explicitly shown.

Cheers,
_

Grzyboo
5th September 2015, 16:25
Like this?



QLayout *layout = ui->scrollArea->widget()->layout();
QTextEdit* text = new QTextEdit(ui->scrollArea);

if(isCode)
{
QHBoxLayout* hLayout = new QHBoxLayout();
hLayout->addItem(new QSpacerItem(16, 64, QSizePolicy::Fixed, QSizePolicy::Fixed));
hLayout->addWidget(text);
layout->addItem(hLayout);
text->show();
}
else
layout->addWidget(text);


Widgets added after else statement just work fine. But layout inside if doesn't. TextEdits appear properly, but scrolling the area doesn't make them move. Do i have to call show() on all the widgets every time an user uses the scrollbar?

anda_skoa
5th September 2015, 17:19
As asked earlier, which type of layout is layout?
If it is a QVBoxLayout, can you try casting to that and call addLayout(hLayout)?

Also either do not specify a parent to the text edit or use the correct parent.

[QUOTE=Grzyboo;281638]
Do i have to call show() on all the widgets every time an user uses the scrollbar?
No.

Cheers,
_

Grzyboo
5th September 2015, 17:28
If it is a QVBoxLayout, can you try casting to that and call addLayout(hLayout)?


This. Thanks ;)