Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to set scroll bar to a stacked widget?



  • Hi All,I m new to qt and have so far used the designer mode to brings items in place.I have a stacked widget with several items.

    StackedWidget
    |_Item1
    |_Item2
    |_Item3
    I want to add a scroll bar(both horizontal and vertical ) to my stackedwidget.

    Things I have done:

    1. In the designer,I have the below but its showing the scrollbars ,but not getting scrolled.

         ScrollArea
          |_QWidget                                          
           |_StackedWidget
      

    2.I had only StackedWidget in my GUI and added the scrollarea and QWidget programmatically.

         QWidget * mainWidget = new QWidget(ui->stackedWidget);
         QVBoxLayout * vLayout = new QVBoxLayout(mainWidget);
         QScrollArea * scrollArea = new QScrollArea(mainWidget);
    
         scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
         scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
         scrollArea->setWidgetResizable(true);
    
         vLayout->addWidget(scrollArea);
    

    But now,the scrollbar is not getting shown.Any help woould be really helpful.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Something is not clear, do you want your stacked widget to be in a QScrollArea programmatically ? Or have the scroll area to be inside the QStackedWidget ?


Log in to reply