Reputation: 754
In my example, I only see the first label - that's on page 1 of the stacked widget. I see no "caption" for the stacked widget's page(s), either.
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();
}
Here's a screenshot. I'm using Qt 5 and Windows 10.
Update:
I was corrected, that my understanding of QStackedWidget was wrong. I was hoping to get something like this:
Upvotes: 0
Views: 65
Reputation: 754
I managed to do what I wanted. It might be called an accordeon. Here's how I did it:
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
}
With some suggar added, this might look like:
Upvotes: 0