添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Your browser does not seem to support JavaScript. As a result, your viewing experience will be diminished, and you have been placed in read-only mode . Please download a browser that supports JavaScript, or enable it if it's disabled (i.e. NoScript). I am new in Qt. I want to set scrollbar to the QVBoxLayout. I have added the following code.

  • setGeometry(0, 0, 240, 320);
  • QWidget *win = new QWidget();
  • setCentralWidget(win);
  • QVBoxLayout *verBoxLayout= new QVBoxLayout(win);
  • QLabel *lbl=new QLabel(tr("Test"));
  • verBoxLayout->addWidget(lbl);
  • My question is that how to add a vertical scrollbar to the Vertical Layout and make it visible all time.

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    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));
        vBoxLayout->addWidget(label);
    QScrollArea *scrollArea = new QScrollArea(this);
    scrollArea->setWidget(window);
    setCentralWidget(scrollArea);
    

    Sample Output:-

    For more information about QScrollArea[https://doc.qt.io/qt-5/qscrollarea.html]

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