Important: Please read the Qt Code of Conduct -

Scroll area in qstackedwidget

  • Hello. One of the last issues in my first application. It is a qstackedwidget with several panels in it. The size of the mainwindow is 1280 by 760. When someone opens the application on a low-res computer, a part of the application is cut off. So what I want to do is to have a maximum size set to 1280 by 800 and the minimum size set to 800 by 600. So when a user opens it on a low-res s/he can downsize it. Then the idea is that when the application is downsized, the user has scroll bars (vertical and horizontal). But only when the size is less than the maximum.

    I have inserted the following in the constructor:

    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)

    QScrollArea *scrollarea = new QScrollArea;


    I am not even sure whether this is correct as I get the following error:

    no matching function for call to QScrollArea::setHorizontalScrollBar

    I was wondering if my approach is correct. I also tried to add a scroll area in Qt Creator and move my widgets there. But what I get is a scroll area with scroll bars in the maximum size that do not appear when the size is reduced. I have no idea.

    Thanks a lot!

  • setHorizontalScrollBarPolicy() not setHorizontalScrollBar()

    The size available to the scroll area is driven by the layout it is placed in. The presence of scroll bars is driven by the size of the widget contained in the scroll area. If that widget is larger than the space available to the scroll area then you get scroll bars. An empty QStackedWidget likely has a very small default size, hence no scroll bars.

  • You are calling the wrong function. That is why you are getting the errors.


  • Thank you, guys. Now I have no errors. However, when it is compiled, I do not see anything but an empty window. No stackedwidget panels, no scrollbars. If I use one of the pages in the stackewidget as a parameter, like:


    this page is not visible, I see the next one instead.

    Anything else that I forgot to do?

    Thanks a lot!

  • Have you actually put the QScrollArea into the widget layout where the ui->stackedWidget was?

    You can do all of this is Designer if you wish.

  • Hi, Chris. No, I have not. I tried to do it using the code above. Now I have tried what you suggested, i.e. inserting the scrollarea in the Designer. Well, it kind of worked. A different problem this time. If I do not have setMinimumSize, then the window only shrinks a little bit, although I do see the scroll bars. But it does not shrink to the necessary minimum size of 800X600. Now, if I have setMinimumsize(800,600), I can reduce the main window to this size but the scrollarea is not see at it. It is seen somewhere in between (actually at the size that I can shrink it to in the first scenario), but as I go reducing the size, the scrollbars disappear again. M-m-m. :-(

    EDIT: I think I have found it: somehow the CentralWidget had the minimum size set to 1200x700. This might have been solved for now.

    Thank you all for your help!

Log in to reply