I have a grid layout with a bunch of QPushButtons and my own flavor of QToolButtons in it. My dialog is displaying to my taste except when I happen to have too many buttons in it which causes the grid to expand beyond the screen size. So I want to add a scroll bar.
I've already read a bunch a questions/solutions about scroll bars and QtGridLayout but after implementing this accepted solution for example qdialog with scrollarea and gridlayout all my buttons (which should have their size fixed - corresponding code omitted below for clarity) get shrunk to a stupid size. Also, when I resize my dialog, the scroll area resizes accordingly, but the grid inside it doesn't. What am I missing?
NOTE: I have tried all sorts of ways to fix the various dimensions, basically everything works without the scroll area, but as soon as I put things in the scroll area, the grid cells get automatically and nonsensically resized...
void createDatabaseWindow()
{
_database_widget = new QDialog;
QVBoxLayout* layout(new QVBoxLayout);
_database_layout = new QGridLayout;
QLabel* text(new QLabel("some text"));
layout->addWidget(text);
QVBoxLayout* vlayout(new QVBoxLayout);
_new_user_button = new QPushButton("New User");
vlayout->addWidget(_new_user_button);
QPushButton* cancel_button(SM_NEW QPushButton("Cancel"));
vlayout->addWidget(cancel_button);
_database_layout->addLayout(vlayout, 0, 0);
std::vector<Result> database = getWholeDatabase();
for (int i = 0; i < database.size(); i++){
myQToolButton* button(new myQToolButton(database[i]));
_database_layout->addWidget(button, (i + 1) / N_ROWS_DATABASE, (i + 1) % N_ROWS_DATABASE);
}
// the following comes from an accepted solution, but doesn't work for me...
//Create a widget and set its layout as your new layout created above
QWidget *viewport = new QWidget;
viewport->setLayout(_database_layout);
//Add the viewport to the scroll area
QScrollArea *scrollArea = new QScrollArea;
scrollArea->setWidget(viewport);
//Add the scroll area to your main window's layout
layout->addWidget(scrollArea);
_database_widget->setLayout(layout);
}