PDA

View Full Version : QScrollArea inner widget problem



agaf
19th January 2010, 15:57
I need QScrollArea to contains some images


TestScrollArea::TestScrollArea(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);

QWidget* widget = new QWidget;
QVBoxLayout* layout = new QVBoxLayout( widget);

QImage image("D:\\data\\photo\\me\\bg2009.jpg");
Q_ASSERT( image.isNull() == false);

QLabel* label1 = new QLabel;
label1->setPixmap( QPixmap::fromImage( image));
layout->addWidget( label1);

ui.scrollArea->setBackgroundRole(QPalette::Dark);
ui.scrollArea->setWidget( widget);
}

This code was placed to the main window constructor and it worked ok (image displayed). Then I created QScrollArea child class and moved the code to its contructor:


TestImageViewer::TestImageViewer(QWidget *parent)
: QScrollArea(parent)
{
QWidget* widget = new QWidget;
QVBoxLayout* layout = new QVBoxLayout( widget);

QImage image("D:\\data\\photo\\me\\bg2009.jpg");
Q_ASSERT( image.isNull() == false);

QLabel* label1 = new QLabel;
label1->setPixmap( QPixmap::fromImage( image));
layout->addWidget( label1);

setBackgroundRole(QPalette::Dark);
setWidget( widget);
}

Now I see nothing except a grey rectangle. What I'm doing wrong?

mattc
19th January 2010, 17:33
The way you create your layout is odd, why are you passing the widget as parent? Usually, you want to create the layout with no parent and then call:

widget->setLayout(layout);
See code here:
http://doc.trolltech.com/4.6/qvboxlayout.html#details

edit: oops, I just read you can pass the widget as parent. However, setLayout() is worth a try :-)

Coises
19th January 2010, 17:53
Now I see nothing except a grey rectangle. What I'm doing wrong?Just a possibility:

Are you sure that when you created the TestImageViewer class, you made the corresponding changes (presumably in Qt Designer) so that ui.setupUi is constructing a TestImageViewer where it used to construct a plain QScrollArea?

agaf
19th January 2010, 19:06
Just a possibility:

Are you sure that when you created the TestImageViewer class, you made the corresponding changes (presumably in Qt Designer) so that ui.setupUi is constructing a TestImageViewer where it used to construct a plain QScrollArea?

Yes, that was I checked first. To say the truth in the first case "empty" TestImageViewer is used as well