I'm creating dynamically unknown number of buttons and put them to QScrollArea.
someDialogButton = new QPushButton();
usersDialogs.push_back(someDialogButton);
ui->usersArea->setWidget(someDialogButton);
usersDialogs
is QVector<QPushButton*>
usersArea
is QScrollArea
But the buttons overlap and can be seen only the last button added. I tried use: setLayout(QLayout* layout)
method, but it was useless.
How I can compose the buttons to make all buttons visible?
The Qt documentation says
void QScrollArea::setWidget(QWidget * widget)
Sets the scroll area's widget.
The widget becomes a child of the scroll area, and will be destroyed when the scroll area is deleted or when a new widget is set.
Therefore, whenever you call QScrollArea::setWidget()
you overwrite the previous widget. What you need to do is to fill a QWidget
with the buttons and then you call QScrollArea::setWidget()
on that container widget.
Here's an example, of what your classes constructor could look like:
MainWindow::MainWindow(QWidget *parent)
: QWidget(parent)
{
this->resize( 300, 200 );
auto * layout = new QVBoxLayout(this);
auto * scrollArea = new QScrollArea(this);
scrollArea->setWidgetResizable( true );
layout->addWidget( scrollArea );
auto * container = new QWidget();
scrollArea->setWidget( container );
layout = new QVBoxLayout(container);
auto * button1 = new QPushButton( "1", container);
auto * button2 = new QPushButton( "2", container);
auto * button3 = new QPushButton( "3", container);
layout->addWidget( button1 );
layout->addWidget( button2 );
layout->addWidget( button3 );
}
For me it gives this: