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

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

  • Qt Champions 2016

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

    Note the viewport thing.

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

  • Qt Champions 2016

    @ModelTech said:


    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

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

  • Qt Champions 2016

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

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


    and this is the effect with the setGeometry call:


  • Qt Champions 2016

    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.

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

  • Qt Champions 2016

    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 ?

  • 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"));
        QLabel *WeightLabel = new QLabel(tr("Weight"));
        QHBoxLayout *Heading = new QHBoxLayout;
        QWidget *Profiles = new QWidget;
        QGridLayout *ProfilesGrid = new QGridLayout;
        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()));
            ProfilesGrid->addWidget(TimeEditor, i, 0);
            QLineEdit *WeightEditor = new QLineEdit(QString("%1").arg(Specification->profile(i)->weight()));
            ProfilesGrid->addWidget(WeightEditor, i, 1);
            QToolButton *DeleteButton = new QToolButton;
            ProfilesGrid->addWidget(DeleteButton, i, 2);
        QScrollArea *ScrollArea = new QScrollArea;
        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"));
        QVBoxLayout *Layout = new QVBoxLayout;

Log in to reply

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