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. Scroll area in qstackedwidget
Qt 6.11 is out! See what's new in the release blog

Scroll area in qstackedwidget

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 4.4k 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.
  • I Offline
    I Offline
    igorland
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        steno
        wrote on last edited by
        #3

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

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

        1 Reply Last reply
        0
        • I Offline
          I Offline
          igorland
          wrote on last edited by
          #4

          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!

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • I Offline
              I Offline
              igorland
              wrote on last edited by
              #6

              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!

              1 Reply Last reply
              0

              • Login

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