题
我有一个QDialog,我想在10 x 5网格中显示50个Qcomboboxes。由于这么多组合盒不适合我的对话框,因此我想使用滚动。
这是我尝试过的,但这对我不起作用。我什至朝这个解决方案朝正确的方向前进吗?
// 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
解决方案
是的,您正朝正确的方向前进。做下面的事情
//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);
不隶属于 StackOverflow