QT ScrollBars doesn't appear with scrollAreaWidgetContents



  • I am trying to learn QT with visual studio. My development environment is VS2015 with QT designer. I am trying to do the following tutorial.

    http://www.bogotobogo.com/Qt/Qt5_QMainWindow_QAction_ImageViewer_B.php

    essentially the tutorial displays an image with Qlabel and QScrollArea. If I create the widgets with code (using visual studio) the program seems to work.

     imageLabel = new QLabel;
     imageLabel->setBackgroundRole(QPalette::Base);
     imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
     imageLabel->setScaledContents(true);
    
     scrollArea = new QScrollArea;
     scrollArea->setBackgroundRole(QPalette::Dark);
     scrollArea->setWidget(imageLabel);
     setCentralWidget(scrollArea);
    

    However, then I tried to do the same with QT Designer. When I dragged a ScrollArea from QT Designer, I noticed that it also created a QWidget called "scrollAreaWidgetContents". Then when I created the QLabel, Label was created under "scrollAreaWidgetContents" widget. I then tried to display the image using the following code.

    ui->label->setBackgroundRole(QPalette::Base);
    ui->label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    ui->label->setScaledContents(true);
    
    ui->scrollArea->setBackgroundRole(QPalette::Dark);
    ui->scrollArea->setWidget(ui->scrollAreaWidgetContents);
    ui->scrollArea->setWidgetResizable(true);
    ui->scrollAreaWidgetContents->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    //ui->scrollArea->setWidget(ui->label);
    setCentralWidget(ui->scrollArea);
    

    The problem with this is that the scroll Bars never appears.

    Then I uncommented "ui->scrollArea->setWidget(ui->label);" line and then the program compiles but crashes. So, how do I make the scrollBars appear with QT Designer? What am I doing wrong.

    Thanks for the help


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    IIRC, you can use the "promote to" feature and make that widget a QLabel.

    Hope it helps



  • I tired it but it didn't let me promote QWidget ("scrollAreaWidgetContents") to QLabel. It said QLabel aleardy exists. I tried it using QT Designer.


  • Lifetime Qt Champion

    An alternative could be to just edit the .ui file directly and change scrollAreaWidgetContents to a QLabel. Would likely be faster.



  • I know this is an old thread but I just want to chip in to this.

    I got this exact problem trying to put anything into a QScrollBar in designer and they just don't work because of this stupid dummy "scrollAreaWidgetContents" that was auto created. It cannot be modified or deleted.

    The only workaround I found is to do everything in the main cpp, creating the image label and adding it to the QScrollArea object, skipping the scrollAreaWidgetContents, which completely took away the convenience of using the designer.


Log in to reply