Prevent widgets stretching in QVBoxLayout and have scrollbar appear in
QScrollArea?
Making my way up the Qt learning curve, I've seen many questions about
dynamic layouts but the solutions aren't working for me or I don't quite
understand them.
Reference questions:: Qt Scroll Area does not add in scroll bars, How can
i make widgets overflow to make a scrollbar appear in Qt?
Question:: I want to have a dynamic layout of a set of widgets within a
QScrollArea. I've been able to do this manually in Qt Creator and now I am
trying to do it through code.
How do I prevent the widgets from stretching/force the area to scroll?
How do I have the added widgets start from the top? I have a vertical
spacer in my QVBoxLayout but that pushes everything to the bottom.
Simple test code::
void MainWindow::on_pushButton_clicked()
{
ui->myScroll->setWidgetResizable(true); //making sure this is set
QPushButton *b = new QPushButton(this);
b->setText(QString("Hello Button"));
QHBoxLayout *h = new QHBoxLayout();
h->addWidget(b,0);
ui->myVBoxLayout->addLayout(h,0);
}
Result:: Left side squished (dynamic) – Right side Ok (set up manually)
Qt Creator Setup:: Left side: dynamic – Right side set up manually
Properties::
No comments:
Post a Comment