我的程序有一个主窗口,该窗口创建了一个名为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();

最佳答案

您必须创建一个包含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();
输出:
c&#43;&#43; - 如何将QFormLayout放在scrollArea中?-LMLPHP
在下面的link中,有一个示例。

关于c++ - 如何将QFormLayout放在scrollArea中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49099606/

10-12 18:26