PDA

View Full Version : QT ImageViewer Tutorial crashes with QT Designer



nipunamr
27th October 2017, 17:27
My development environment is Visual studio 2015 with QT 5.6.3. I am creating forms using QT designer. I am doing several QT tutorials; specifically I am doing the following tutorial.

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

The program essentially, displays an image using QScrollArea and QLabel. The program works if I create my "widgets" in visual studio (without QT designer)

//Here's the relevant code


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



As expected the scrollbars appear.

Then I created the widgets in QT Designer and tried to use it in visual studio. The one thing I noticed is that when I create a scrollArea using QT designer it also creates a QWidget called "ScrollAreaWidgetContents". See the attached "QT_Designer_1" and "QT_Inspector_Window" images. The problem is that I can't see scroll bars with this program. See the attached "Image_Viewer_No_ScrollBars". Here's the releavant 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);



I then tried to uncomment "ui->scrollArea->setWidget(ui->label)" line and the program crashes. See the attached "Image_Viewer_Crashes" I guess I don't understand why it's crashing. Could anyone help resolve this problem?

Thanks

geekowl
31st October 2017, 06:58
On designer as default scrollarea creating a central widget. This is the auto-generated code from designer and if you put your label into this content it will work. Or you can create your own class inherited from QLabel and promote that contents widget into your class and that will work too.




scrollAreaWidgetContents = new QWidget();
scrollAreaWidgetContents->setObjectName(QStringLiteral("scrollAreaWidgetContents"));
scrollAreaWidgetContents->setGeometry(QRect(0, -460, 1920, 1080));
verticalLayout = new QVBoxLayout(scrollAreaWidgetContents);
verticalLayout->setSpacing(6);
verticalLayout->setContentsMargins(11, 11, 11, 11);
verticalLayout->setObjectName(QStringLiteral("verticalLayout"));
verticalLayout->setContentsMargins(0, 0, 0, 0);
label = new QLabel(scrollAreaWidgetContents);
label->setObjectName(QStringLiteral("label"));
label->setPixmap(QPixmap(QString::fromUtf8("../../../Pictures/chuukarudoruhu.jpg")));