PDA

View Full Version : Positioning elements in QScrollArea



Ironus
29th January 2015, 17:38
First of all, hello everyone!

I'm not a native speaker, so really sorry for my english. I'll do my best to be as clear as only possible.

I've started my journey with Qt few days ago, so I'm not very experienced with it. I'm trying to make simple application with QScrollArea and QTextEdit fields.
I want to add QPushButtons to QScrollArea dynamically - on button press. Everything works fine, but positioning elements in QScrollArea is ruined when there is less than few buttons added (everything explained in screenshot in attachments). I've tried to setContentsMargins(0, 0, 0, 0) and setAligment(Qt::AlignTop), but it doesn't work as I thought it will be.

Here is my code where I'm adding QScrollArea:


numberOfGridBoxRows = 0;

gridGroupBox = new QGroupBox(tr("Próbki"));
layout = new QGridLayout;

samplesWidget = new QWidget();
samplesButtonsLayout = new QGridLayout();
samplesButtonsLayout->setContentsMargins(0, 0, 0, 0);
samplesWidget->setLayout(samplesButtonsLayout);

samplesScroller = new QScrollArea();
samplesScroller->setWidgetResizable(true);
samplesScroller->setContentsMargins(0, 0, 0, 0);
samplesScroller->setAlignment(Qt::AlignTop);
samplesScroller->setWidget(samplesWidget);

smallEditor = new QTextEdit;
smallEditor->setPlainText(tr(""));
smallEditor->setMinimumWidth(300);
smallEditor->setMinimumHeight(300);

layout->addWidget(samplesScroller, 0, 0, 1, 1);
layout->addWidget(smallEditor, 0, 1, 1, 1);

layout->setColumnStretch(0, 100);
layout->setColumnMinimumWidth(0, 150);
layout->setColumnStretch(1, 60);

gridGroupBox->setLayout(layout);

And here is code where I'm adding QPushButtons to QScrollArea widget (part of another function):


// I've changed parameters in button constructor here, because normally as a button name is value provided by user
QPushButton *showSampleButton = new QPushButton(tr("button"), this);
samplesScroller->ensureWidgetVisible(samplesWidget, 0, 0);
samplesButtonsLayout->addWidget(showSampleButton, numberOfGridBoxRows++, 0, 1, 1, Qt::AlignTop);


I'll be very thankfull for help.
Best regards,
Ironus
10915

Radek
29th January 2015, 18:46
Add a vertical spacer below the buttons. Start with the spacer only and insert buttons before the spacer. You reach the same effect as if you added buttons to the end.

Ironus
29th January 2015, 19:10
I've changed QGridLayout to QVBoxLayout and added spacer as you suggested.
Works like a charm! Thank you for quick answer!

Is there any possibility to change spacing between buttons? I've set contents margin to (0, 0, 0, 0), but it doesn't change anything.

Best regards,
Ironus
10916

wysota
29th January 2015, 19:23
Is there any possibility to change spacing between buttons?

Yes. Adjust the spacing property for the layout.

Ironus
29th January 2015, 19:29
Och, ok, silly me. I checked documentation only for QVBoxLayout and there was only setContentsMargin(0,0,0,0);
I haven't thought about checking QLayout documenation.
Now setSpacing works!
Thank you for help!