如何在scrollArea中放置QFormLayout?

时间:2018-03-04 19:39:49

标签: c++ qt qt5 qscrollarea qformlayout

我的程序有一个主窗口,可以创建一个名为wdg的小部件。此小部件的QFormLayout包含193行。我希望能够向下滚动QFormLayout。我尝试制作QScrollArea并将其与布局和窗口小部件集成,但这会导致窗口小部件无法显示或滚动条未显示。我认为这与新窗口的高度似乎与整个布局一样长并且离开屏幕的事实有关。我尝试将窗口小部件的几何设置为有限的大小,但它只是改变了位置而不是大小。 窗口小部件中的结果未显示:

QWidget *wdg = new QWidget;
QScrollArea *scroll = new QScrollArea;
QFormLayout *formLayout = new QFormLayout(wdg);
int lenght = keys.size();
        for(int x=0; x<=lenght-1; x++)
{

            QComboBox* combo = new QComboBox;
            combo->addItem("Present");
            combo->addItem("Present and Voting");
            combo->addItem("Absent");
            combo->addItem("Absent from Commitee");
            combo->setProperty("MyIndex", x);
            combo->setCurrentIndex(status[x]);
 formLayout->addRow(keys.at(x),combo);
connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(roll(int)));

}
scroll->setWidget(wdg);
 wdg->setLayout(formLayout);

wdg->show();

小部件显示但没有滚动条或布局的结果:

QWidget *wdg = new QWidget;
QScrollArea *scroll = new QScrollArea(wdg);
QFormLayout *formLayout = new QFormLayout();
int lenght = keys.size();
        for(int x=0; x<=lenght-1; x++)
{

            QComboBox* combo = new QComboBox;
            combo->addItem("Present");
            combo->addItem("Present and Voting");
            combo->addItem("Absent");
            combo->addItem("Absent from Commitee");
            combo->setProperty("MyIndex", x);
            combo->setCurrentIndex(status[x]);
 formLayout->addRow(keys.at(x),combo);
 connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(roll(int)));

}
scroll->setLayout(formLayout);


wdg->show();

1 个答案:

答案 0 :(得分:1)

您必须创建包含QFormLayout的窗口小部件,然后将该窗口小部件设置为QScrollArea,并且QScrollArea您必须在窗口小部件wdg中设置它另一种布局:

QWidget *wdg = new QWidget;
QScrollArea *scroll = new QScrollArea;

QWidget *content_widget = new QWidget;

QFormLayout *formLayout = new QFormLayout(content_widget);

QStringList items{"Present", "Present and Voting", "Absent", "Absent from Commitee"};

for(int x=0; x < keys.size(); x++)
{

    QComboBox *combo = new QComboBox;
    combo->addItems(items);
    combo->setProperty("MyIndex", x);
    combo->setCurrentIndex(status[x]);
    formLayout->addRow(keys.at(x),combo);
    connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(roll(int)));
}

scroll->setWidget(content_widget);
wdg->setLayout(new QVBoxLayout);
wdg->layout()->addWidget(scroll);
wdg->show();

输出:

enter image description here

在以下link中有一个例子。