带有scrollarea和gridlayout的qdialog

时间:2011-07-25 20:32:32

标签: qt qdialog

我有一个QDialog,我希望在10 x 5网格中显示50个QComboBox。由于这么多的组合框不适合我的对话框,我想使用滚动。

这是我尝试过的,但这对我不起作用。我是否正朝这个解决方案走向正确的方向?

// 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);

感谢所有帮助 Dhotiwalla

1 个答案:

答案 0 :(得分:3)

是的,你正朝着正确的方向前进。做一些如下的事情

//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);