【问题标题】:qdialog with scrollarea and gridlayout带有滚动区域和网格布局的 qdialog
【发布时间】:2011-07-25 20:32:31
【问题描述】:

我有一个 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);

感谢所有帮助 多蒂瓦拉

【问题讨论】:

    标签: qt qdialog


    【解决方案1】:

    是的,您正朝着正确的方向前进。执行以下操作

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-04
      • 1970-01-01
      • 2018-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多