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:

    [code]
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    this->setMaximumSize(1280,800);
    this->setMinimumSize(800,600);

    QScrollArea *scrollarea = new QScrollArea;
    scrollarea->setWidget(ui->stackedWidget);
    scrollarea->setHorizontalScrollBar(Qt::ScrollBarAsNeeded);
    scrollarea->setVerticalScrollBar(Qt::ScrollBarAsNeeded);
    scrollarea->setWidgetResizable(true);
    

    [/code]

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

    [code]
    no matching function for call to QScrollArea::setHorizontalScrollBar
    [/code]

    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.

    @
    scrollarea->setHorizontalScrollBarPolicy((Qt::ScrollBarAsNeeded);
    scrollarea->setVerticalScrollBarPolicy((Qt::ScrollBarAsNeeded);
    @



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

    [code]
    scrollarea->setWidget(ui->page1);
    [/code]

    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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.