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


  • Qt Champions 2016

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

    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



  • 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

    @ModelTech
    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:

    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


  • Qt Champions 2016

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



  • 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

    @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 ?



  • 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);
    

Log in to reply
 

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