How to implement collapsible sidebar with QSplitter that can be toggled with a button
Unsolved
General and Desktop
-
I am trying to implement a type of collapsible sidebar with a
QSplitter
and aQButton
that collapses/expands the sidebar (the leftmost widget in myQSplitter
. However, I am having problem to make the "sidebar" collapse to the width of the button for collapse/expand. I triedvoid QSplitter::setSizes(const QList<int> &list)
and I do not even remember what else, but up to now I was not able to figure out how to do it. Does anybody have an idea, what is missing?This is my code:
// main.cpp #include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
// mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> class QSplitter; class QPushButton; class QVBoxLayout; class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void collapseExpand(bool checked); private: QVBoxLayout* verticalLayout; QPushButton * button; QString buttonText; QSplitter * splitter; }; #endif // MAINWINDOW_H
//mainwindow.cpp #include "mainwindow.h" #include <QDebug> #include <QListView> #include <QLabel> #include <QSplitter> #include <QVBoxLayout> #include <QHBoxLayout> #include <QPushButton> #include <QIcon> #include <QSpacerItem> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), buttonText("Collapse") { QWidget* cWidget = new QWidget(this); verticalLayout = new QVBoxLayout(cWidget); verticalLayout->setObjectName("verticalLayout"); button = new QPushButton(QIcon("apple.jpg"), buttonText, cWidget); button->setCheckable(true); button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); QSpacerItem* spacerItem = new QSpacerItem(1, 1, QSizePolicy::Fixed, QSizePolicy::MinimumExpanding); verticalLayout->addWidget(button); verticalLayout->addWidget(new QLabel("This is the first label which is very long")); verticalLayout->addWidget(new QLabel("This is the second label which is very long")); verticalLayout->addWidget(new QLabel("This is the third label which is very long")); verticalLayout->addWidget(new QLabel("This is another label that is very long")); verticalLayout->addItem(spacerItem); splitter = new QSplitter(this); QLabel * myLabel = new QLabel("My label"); setCentralWidget(splitter); splitter->addWidget(cWidget); splitter->addWidget(myLabel); splitter->setCollapsible(0, false); for (int i=0; i<splitter->sizes().count(); i++) { splitter->setStretchFactor(i, 0); } QObject::connect(button, &QPushButton::clicked, this, &MainWindow::collapseExpand); } MainWindow::~MainWindow() { } void MainWindow::collapseExpand(bool checked) { for (int i = 0; i < verticalLayout->count(); ++i) { QWidget * wdg = verticalLayout->itemAt(i)->widget(); if (wdg && button != wdg) { wdg->setVisible(!checked); } } if (checked) { button->setText(""); } else { button->setText(buttonText); } }
-
I use the following and it works:
pcoSplitter->setSizes({ pcoSplitter->size().width(), 0 });