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. Vertical Scroll Bar to QVBoxLayout
Forum Updated to NodeBB v4.3 + New Features

Vertical Scroll Bar to QVBoxLayout

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 5.1k 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.
  • R Offline
    R Offline
    Rumzi
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

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

      1 Reply Last reply
      4
      • Shrinidhi UpadhyayaS Offline
        Shrinidhi UpadhyayaS Offline
        Shrinidhi Upadhyaya
        wrote on last edited by
        #3

        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:-

        0_1560232586030_489b7600-46c6-494b-8047-4c617637b166-image.png

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

        Shrinidhi Upadhyaya.
        Upvote the answer(s) that helped you to solve the issue.

        1 Reply Last reply
        5
        • R Offline
          R Offline
          Rumzi
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • Pradeep P NP Offline
            Pradeep P NP Offline
            Pradeep P N
            wrote on last edited by Pradeep P N
            #5

            Hi @Rumzi

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

            Example:

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

            0_1560839842179_8.png

            Pradeep Nimbalkar.
            Upvote the answer(s) that helped you to solve the issue...
            Keep code clean.

            1 Reply Last reply
            2

            • Login

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