gpt4 book ai didi

c++ - 如何将 QFormLayout 放入 scrollArea 中?

转载 作者:行者123 更新时间:2023-11-28 04:46:56 25 4
gpt4 key购买 nike

我的程序有一个主窗口,它创建了一个名为 wdg 的小部件。这个小部件有一个包含 193 行的 QFormLayout。我希望能够向下滚动 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();

输出:

enter image description here

在下面link有一个例子。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com