在我的示例中,我只看到第一个标签 - 位于堆叠小部件的第 1 页上。我也没有看到堆叠小部件页面的“标题”。
class QTest : public QDialog {
public:
QTest(QWidget* parent = nullptr) : QDialog(parent) {
setWindowTitle("Stacked Widget Example");
QVBoxLayout* mainLayout = new QVBoxLayout(this);
QStackedWidget* stackedWidget = new QStackedWidget(this);
const int numPages = 5;
for (int i = 0; i < numPages; ++i) {
auto* optionsPage = new QWidget(this);
auto* formLayout = new QFormLayout(optionsPage);
optionsPage->setWindowTitle(QString("Page %1").arg(i + 1));
formLayout->addRow(new QLabel(QString("Label %1").arg(i + 1)), new QLineEdit(this));
stackedWidget->addWidget(optionsPage);
}
mainLayout->addWidget(stackedWidget);
setLayout(mainLayout); // Ensure layout is set to the dialog
}
};
void foo() {
auto* pp = new QTest(this);
pp->exec();
}
这是一个屏幕截图。我正在使用 Qt 5 和 Windows 10。
更新:
我成功地做了我想做的事。它可能被称为手风琴。我是这样做的:
SettingsDialog::SettingsDialog(QWidget* parent, QString title, const std::vector<QString>& items):QDialog(parent) {
QVBoxLayout* mainLayout = new QVBoxLayout(this);
// Create a container widget to hold the sections
QWidget* containerWidget = new QWidget(this);
QVBoxLayout* containerLayout = new QVBoxLayout(containerWidget);
containerWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Maximum);
QFormLayout* formLayout = nullptr;
for(auto& it : m_items) {
if(it[0] == "*") { // new group
// header button to enable/disable this group
QPushButton* toggleButton = new QPushButton(it, this);
containerLayout->addWidget(toggleButton);
QWidget* contentWidget = new QWidget(this);
formLayout = new QFormLayout(contentWidget);
contentWidget->setLayout(formLayout);
contentWidget->setVisible(true);
connect(toggleButton, &QPushButton::clicked, [contentWidget]() {
contentWidget->setVisible(!contentWidget->isVisible());
});
// Set no margins or spacing for the containerLayout
containerLayout->setContentsMargins(0, 0, 0, 0);
containerLayout->setSpacing(0);
// Add stacked widget to the main layout
containerLayout->addWidget(contentWidget);
it.m_pWidget = contentWidget;
continue;
}
auto* pWidget = new QLineEdit(this);
formLayout->addRow(new QLabel(it.m_displayName), pWidget);
}
// Create a scroll area and set the container widget as its widget
QScrollArea* scrollArea = new QScrollArea(this);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
scrollArea->setWidget(containerWidget);
scrollArea->setWidgetResizable(true);
mainLayout->addWidget(scrollArea);
// Add OK and Cancel buttons
// Create buttons
QPushButton* okButton = new QPushButton("OK", this);
QPushButton* cancelButton = new QPushButton("Cancel", this);
// Button layout
QHBoxLayout* buttonLayout = new QHBoxLayout;
buttonLayout->addStretch();
buttonLayout->addWidget(okButton);
buttonLayout->addWidget(cancelButton);
mainLayout->addLayout(buttonLayout);
// Connect buttons
connect(okButton, &QPushButton::clicked, this, [&]() {onOK();});
connect(cancelButton, &QPushButton::clicked, this, &QDialog::reject);
setMinimumSize(320, 200); // don't like to see a horizontal scrollbar
}
添加一些糖,可能看起来像: