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. QSizePolicy::Maximum on QScrollArea puts Scrollbar on top of containing Widget

QSizePolicy::Maximum on QScrollArea puts Scrollbar on top of containing Widget

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 2 Posters 3.3k Views
  • 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.
  • ModelTechM Offline
    ModelTechM Offline
    ModelTech
    wrote on last edited by
    #1

    I am trying to make a ScrollArea with a vertical scrollbar only. The width of the complete ScrollArea should be such that the complete width of the containing widget is always visible. To this end, I wrote the following code:

        QScrollArea *ScrollArea = new QScrollArea;
        ScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        ScrollArea->setWidget(MyWidget);
        ScrollArea->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
        ScrollArea->verticalScrollBar()->setSingleStep(HeightOfMyWidget / ItemsInMyWidget);
    

    If the scrollbar is present (which is only the case when the window size is too small), the scrollbar is always partially hiding the content of MyWidget. How can I solve this?

    I have tried to fix the width of the ScrollArea equal to MyWidget.width() + ScrollArea->verticalScrollBar()->width() but that does not give the desired effect, both in case the scrollbar is present and when not --> it gets way too wide in both cases...

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      hi
      this seems to align perfectly
      ui->widget->setGeometry(0,0, ui->scrollArea->viewport()->width(), 200 );

      Note the viewport thing.

      1 Reply Last reply
      0
      • ModelTechM Offline
        ModelTechM Offline
        ModelTech
        wrote on last edited by ModelTech
        #3

        Not in my case... I tried:

        MyWidget->setGeometry(ScrollArea->x(),ScrollArea->y(), ScrollArea->viewport()->width(), ScrollArea->height());
        

        All works fine except for the width. This makes it actually worse...

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @ModelTech said:

          ScrollArea->x(),ScrollArea->y()

          should that not be 0,0 ?

          else it should be
          MyWidget->setGeometry(ScrollArea->x(),ScrollArea->y(), ScrollArea->viewport()->width() - ScrollArea->x(), ScrollArea-height());

          since you offset widget into the viewport. ( if not 0)
          so the WIDTH should be adjusted as if widget x is not zero

          1 Reply Last reply
          0
          • ModelTechM Offline
            ModelTechM Offline
            ModelTech
            wrote on last edited by
            #5

            Unfortunately, that does not help either. Also using 0,0 as x and Y does not change the result. The result is the same as in my previous answer: worse than without the setGeometry call.

            mrjjM 1 Reply Last reply
            0
            • ModelTechM ModelTech

              Unfortunately, that does not help either. Also using 0,0 as x and Y does not change the result. The result is the same as in my previous answer: worse than without the setGeometry call.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @ModelTech
              ok that is strange
              here it makes it fit perfect, with or without scrollbars

              1 Reply Last reply
              0
              • ModelTechM Offline
                ModelTechM Offline
                ModelTech
                wrote on last edited by ModelTech
                #7

                Not sure how to post pictures here, but this is the effect without the setGeometry call:

                http://i513.photobucket.com/albums/t333/bmc-parts/modeltech/Without_zpsosb3dxgy.png

                and this is the effect with the setGeometry call:

                http://i513.photobucket.com/albums/t333/bmc-parts/modeltech/With_zpscpluqazz.png

                mrjjM 1 Reply Last reply
                0
                • ModelTechM ModelTech

                  Not sure how to post pictures here, but this is the effect without the setGeometry call:

                  http://i513.photobucket.com/albums/t333/bmc-parts/modeltech/Without_zpsosb3dxgy.png

                  and this is the effect with the setGeometry call:

                  http://i513.photobucket.com/albums/t333/bmc-parts/modeltech/With_zpscpluqazz.png

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @ModelTech
                  well u cant post images so what u did was the right way.

                  The area above the profile button with the lineedits?

                  im not sure what MyWidget refers to, when looking at the images.

                  1 Reply Last reply
                  0
                  • ModelTechM Offline
                    ModelTechM Offline
                    ModelTech
                    wrote on last edited by
                    #9

                    MyWidget is the content of the ScrollArea under 'Profiles'/'Time'/'Weight'. The Widget is built up as a QGridLayout where the first two columns are QLineEdit's and the last column is a QToolButton (this last column is partly invisible due to the scrollbar being drawn over it. The QLineEdits have a fixed width of 80, so have the 'Time' and 'Weight' QLabels.

                    mrjjM 1 Reply Last reply
                    0
                    • ModelTechM ModelTech

                      MyWidget is the content of the ScrollArea under 'Profiles'/'Time'/'Weight'. The Widget is built up as a QGridLayout where the first two columns are QLineEdit's and the last column is a QToolButton (this last column is partly invisible due to the scrollbar being drawn over it. The QLineEdits have a fixed width of 80, so have the 'Time' and 'Weight' QLabels.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @ModelTech
                      ok, i see.
                      must be something to do with layout.
                      I didnt apply one as i was just scaling widget to fit.

                      so mywidget is the scrollAreaWidgetContents ?

                      1 Reply Last reply
                      0
                      • ModelTechM Offline
                        ModelTechM Offline
                        ModelTech
                        wrote on last edited by
                        #11

                        I guess it's maybe easier to give the concrete code, which is part of the constructor for basically the whole widget below the horizontal line.

                            QLabel *Label = new QLabel(tr("Profiles"));
                        
                            QLabel *TimeLabel = new QLabel(tr("Time"));
                            TimeLabel->setFixedWidth(80);
                            QLabel *WeightLabel = new QLabel(tr("Weight"));
                            WeightLabel->setFixedWidth(80);
                        
                            QHBoxLayout *Heading = new QHBoxLayout;
                            Heading->setSpacing(2);
                            Heading->addWidget(TimeLabel);
                            Heading->addWidget(WeightLabel);
                            Heading->addStretch();
                        
                            QWidget *Profiles = new QWidget;
                            QGridLayout *ProfilesGrid = new QGridLayout;
                            ProfilesGrid->setSpacing(2);
                            ProfilesGrid->setContentsMargins(0, 0, 0, 0);
                            for (int i = 0; i != Specification->numberOfProfiles(); i++) {
                                QLineEdit *TimeEditor = new QLineEdit(QString("%1").arg(Specification->profile(i)->time()));
                                TimeEditor->setFixedWidth(80);
                                ProfilesGrid->addWidget(TimeEditor, i, 0);
                                QLineEdit *WeightEditor = new QLineEdit(QString("%1").arg(Specification->profile(i)->weight()));
                                WeightEditor->setFixedWidth(80);
                                ProfilesGrid->addWidget(WeightEditor, i, 1);
                                QToolButton *DeleteButton = new QToolButton;
                                DeleteButton->setIcon(QIcon(":/icons/delete.png"));
                                ProfilesGrid->addWidget(DeleteButton, i, 2);
                            }
                            Profiles->setLayout(ProfilesGrid);
                        
                            QScrollArea *ScrollArea = new QScrollArea;
                            ScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                            ScrollArea->setWidget(Profiles);
                            ScrollArea->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
                            ScrollArea->verticalScrollBar()->setSingleStep(Profiles->height() / Specification->numberOfProfiles());
                        //    Profiles->setGeometry(0,0, ScrollArea->viewport()->width(), ScrollArea->height());
                        
                            QPushButton *Button = new QPushButton(tr("Add Profile"));
                            Button->setFixedWidth(80);
                        
                            QVBoxLayout *Layout = new QVBoxLayout;
                            Layout->addWidget(Label);
                            Layout->addLayout(Heading);
                            Layout->addWidget(ScrollArea);
                            Layout->addWidget(Button);
                            setLayout(Layout);
                        
                        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