我一直在尝试调整QScrollArea中驻留的QStackedWidget的页面(窗口小部件)的大小。不幸的是,我没有做到。在搜索某些特定人员之前,第1页小部件(full_list_frame
)会自动调整大小。但是在搜索之后,第2页小部件(search_list_frame
)的大小与第1页小部件的大小相同。我期望的是,堆叠式小部件的每个页面都可以根据其布局具有多少个小部件/项目自动调整大小,这意味着如果大小不足以滚动,则无法滚动页面。
还是这个问题是由QScrollArea或其他原因引起的?当我进行硬编码以更改第2页小部件(search_list_frame
)的大小并将其打印出来时。输出显示更改后的大小,但仍未更改屏幕上显示的任何内容。
xxx.cpp
void XXX::initComponents()
{
list_scroll_area = new QScrollArea(this);
//list_scroll_area->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
list_scroll_area->setWidgetResizable(true);
list_scroll_area->setStyleSheet("border: 0px solid black;");
list_scroll_area->setFixedHeight(700);
layout->addWidget(list_scroll_area);
full_list_frame = new QFrame(this);
full_list_layout = new QVBoxLayout;
full_list_frame->setLayout(full_list_layout);
search_list_frame = new QFrame(this);
search_list_layout = new QVBoxLayout;
search_list_frame->setLayout(search_list_layout);
search_spacer = nullptr;
stack = new QStackedWidget(this);
list_scroll_area->setWidget(stack);
stack->addWidget(full_list_frame);
stack->addWidget(search_list_frame);
}
void XXX::insertAllPersonItems()
{
for (auto person : persons)
addPerson(person, full_list_layout);
QSpacerItem* si = new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Expanding);
full_list_layout->addItem(si);
}
void XXX::searchForPerson()
{
list_scroll_area->verticalScrollBar()->setSliderPosition(0);
if (search_edit->text().isEmpty()) {
stack->setCurrentIndex(0);
return;
}
for (auto w : widgets) {
delete w;
widgets.removeOne(w);
}
if (search_spacer != nullptr) {
search_list_layout->removeItem(search_spacer);
delete search_spacer;
}
search_spacer = new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Expanding);
stack->setCurrentIndex(1);
for (auto person : persons) {
if (person.info.legal_name.contains(search_edit->text()))
addPerson(person, search_list_layout);
}
search_list_layout->addItem(search_spacer);
}
之前:
之后: