Pergunta

I have a QDialog inside which I want to display 50 QComboBoxes in a 10 x 5 grid. Since so many combo boxes will not fit into my dialog box, I want to use scrolling.

Here is what I've tried, but this does not work for me. Am I even headed in the right direction with this solution?

// setup scroll area
QScrollArea *scrollArea = new QScrollArea(this);
scrollArea->setWidgetResizable(true);

// setup grid layout
QRect rect;
rect.setX(0);
rect.setY(0);
rect.setWidth(1920);
rect.setHeight(1080);

QGridLayout *gridLayout = new QGridLayout;
gridLayout->setGeometry(rect);

// add servers to scroll area
QComboBox *cmbxServer;
int row = 0;
int col = 0;
for (col = 0; col < 10; col++)
{
    gridLayout->setColumnMinimumWidth(col, 150);
    gridLayout->setColumnStretch(col, 0);
}

for (row = 0; row < 5; row++)
{
    for (col = 0; col < 10; col++)
    {
        cmbxServer = new QComboBox(this);
        cmbxServer->setGeometry(0, 0, 150, 30);
        cmbxServer->addItem("Item 1");
        cmbxServer->addItem("Item 2");
        cmbxServer->addItem("Item 3");
        gridLayout->addWidget(cmbxServer, row, col);
    }
}

gridLayout->addWidget(scrollArea);

thanks for all help Dhotiwalla

Foi útil?

Solução

Yes you are heading in right direction. Do something as below

//Create and populate your layout
QGridLayout *gridLayout = new QGridLayout;

//Create a widget and set its layout as your new layout created above
QWidget *viewport = new QWidget;
viewport->setLayout(gridLayout );

//Add the viewport to the scroll area
QScrollArea *scrollArea = new QScrollArea;
scrollArea->setWidget(viewport);

//Add the scroll area to your main window's layout
mainLayout->addWidget(scrollArea);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top