Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to implement collapsible sidebar with QSplitter that can be toggled with a button

How to implement collapsible sidebar with QSplitter that can be toggled with a button

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tsup
    wrote on last edited by tsup
    #1

    I am trying to implement a type of collapsible sidebar with a QSplitter and a QButton that collapses/expands the sidebar (the leftmost widget in my QSplitter. However, I am having problem to make the "sidebar" collapse to the width of the button for collapse/expand. I tried void 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);
        }
    }
    
    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      Zbigniew-Sch
      wrote on last edited by
      #2

      I use the following and it works:
      pcoSplitter->setSizes({ pcoSplitter->size().width(), 0 });

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved