Important: Please read the Qt Code of Conduct -

Vertical Scroll Bar to QVBoxLayout

  • Hi,
    I am new in Qt. I want to set scrollbar to the QVBoxLayout. I have added the following code.

    1. setGeometry(0, 0, 240, 320);
    2. QWidget *win = new QWidget();
    3. setCentralWidget(win);
    4. QVBoxLayout *verBoxLayout= new QVBoxLayout(win);
    5. QLabel *lbl=new QLabel(tr("Test"));
    6. verBoxLayout->addWidget(lbl);

    My question is that how to add a vertical scrollbar to the Vertical Layout and make it visible all time.

  • Lifetime Qt Champion

    You need to use a QScrollArea instead a QWidget as central widget.

  • Hi @Rumzi , use QScrollArea, set the QWidget to it and then set the QScrollArea as centralWidget.

    Here is the sample code:-

    setGeometry(0, 0, 240, 320);
    QWidget *window = new QWidget(this);
    QVBoxLayout *vBoxLayout= new QVBoxLayout(window);
    for (int i = 0; i < 20; i++)
        QLabel *label=new QLabel(tr("Test ") + QString::number(i));
    QScrollArea *scrollArea = new QScrollArea(this);

    Sample Output:-


    For more information about QScrollArea[]

  • @Shrinidhi-Upadhyaya , thanks bro. it's worked absolutely fine. I need another help to resize the scrollbar width.

  • Hi @Rumzi

    You can use setStyleSheet();
    Reference: Qt Style Sheets.


        QScrollArea *scrollArea = new QScrollArea(this);
        scrollArea->setStyleSheet("QScrollBar:vertical { "
                                  "width: 25px; " // Change as per your requirement


Log in to reply