QScrollArea odd sizes



  • My qscrollarea is changing size sometime after creation and I don't understand why.
    Why is scrollArea->width() = 500 during the first qdebug but its 400 when I press a key later?

    #include "ui_mainwindow.h"
    #include <QHBoxLayout>
    #include <QOpenGLWidget>
    #include <QDebug>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        QWidget *mCentralWidget = new QWidget(this);
        setCentralWidget(mCentralWidget);
    
        QHBoxLayout *mainLayout = new QHBoxLayout;
        mainLayout->setMargin(0);
        mCentralWidget->setLayout(mainLayout);
    
    
        QOpenGLWidget* mCanvas = new QOpenGLWidget(this);
        mCanvas->resize(500, 175);
        mCanvas->setMinimumSize(500, 175);
    
        scrollArea = new QScrollArea;
        scrollArea->setWidget(mCanvas);
        scrollArea->setWidgetResizable(true);
        mainLayout->addWidget(scrollArea);
        scrollArea->resize(500,175);
    
        qDebug() << mCanvas->width();
        qDebug() << scrollArea->width();
    
    }
    
    void MainWindow::keyPressEvent(QKeyEvent * event)
    {
        qDebug() << scrollArea->width();
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

  • Lifetime Qt Champion

    @Fundies said:

    For Me it just looks like all get adjusted to the size of the MainWindow.
    so In the constructor the sizing has not yet fully happened and later
    in keypress it has.

    if I insert
    setGeometry(50,50,500,500);
    just after
    ui->setupUi(this);

    I get same value before and after keypress.



  • I want the mainwindow to adjust to the size of the contents not the otherway around...


  • Lifetime Qt Champion

    @Fundies
    The Mainwindow has the size you make it in forms designer or the size you give it in code.
    It will not adjust size to the widgets it owns so you must do it. They adjust to it.


Log in to reply
 

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