带有滚动区域和网格布局的 qdialog

发布于 2024-11-25 22:10:47 字数 1081 浏览 0 评论 0原文

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

感谢所有帮助 多蒂瓦拉

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生生不灭 2024-12-02 22:10:47

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

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文