如何将 QFormLayout 放入 scrollArea 中?
How can I put a QFormLayout in a scrollArea?
我的程序有一个主程序 window,它创建一个名为 wdg
的小部件。这个小部件有一个 QFormLayout
有 193 行。我希望能够向下滚动 QFormLayout
。我尝试制作 QScrollArea
并将其与布局和小部件集成,但它导致小部件不显示或滚动条不显示。我认为这与新 window 的高度似乎与整个布局一样长并超出屏幕这一事实有关。我尝试将小部件的几何形状设置为有限大小,但它只是改变了位置而不是大小。
结果小部件未显示:
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
您必须在小部件 [=14= 中设置它] 通过另一个布局:
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();
输出:
下面link有个例子
我的程序有一个主程序 window,它创建一个名为 wdg
的小部件。这个小部件有一个 QFormLayout
有 193 行。我希望能够向下滚动 QFormLayout
。我尝试制作 QScrollArea
并将其与布局和小部件集成,但它导致小部件不显示或滚动条不显示。我认为这与新 window 的高度似乎与整个布局一样长并超出屏幕这一事实有关。我尝试将小部件的几何形状设置为有限大小,但它只是改变了位置而不是大小。
结果小部件未显示:
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
您必须在小部件 [=14= 中设置它] 通过另一个布局:
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();
输出:
下面link有个例子