PDA

View Full Version : QScrollArea ->setWidget(this) doesn't produce a ScrollArea. Why?



planetLars
25th April 2010, 14:23
Hello,

putting a Widget inside a QScrollArea despends on where the pointer variable to the QScrollArea is saved. If I have: (Pseudo-Code)


class1():new class2(&class1)

class2():QScrollArea->setWidget(this)

I don't get scrollbars.

If I have:


class1():new class2(&class1),QScrollArea->setWidget(class2)

I get scrollbars.

Why is this?

The only differences is the location of the QScrollArea pointer variable (class1 or class 2). In both cases class2 gets set and class2 should be reparented from class1 to QScrollArea while class1 becomes the parent of QScrollArea.
As an object shouldn't have access to the scope it is placed in, it shouldn't matter whether the pointer variable is in class2 or in class1!

BR

Lars

wysota
25th April 2010, 14:37
Could you provide real code instead of this pseudo-pseudo-code?

planetLars
25th April 2010, 15:33
yes, here it is:

works: test.cpp


#include <QtGui>
#include "test.h"

Test::Test()
{
vBoxLayout = new QVBoxLayout();
t2 = new Test2(this);
t = new QScrollArea();
t->setWidget(t2);
vBoxLayout->addWidget(t);
setLayout(vBoxLayout);
}

int main(int argc, char **argv)
{
QApplication app(argc, argv);
Test* t = new Test();
t->show();
return app.exec();
}

Test2::Test2()
{
s.setWidth(200);
s.setHeight(200);
//t = new QScrollArea();
//t->setWidget(this);
}

QSize Test2::sizeHint() const
{
return s;
}

test.h:


#ifndef TEST_H
#define TEST_H


#include <QWidget>
#include <QVBoxLayout>
#include <QScrollArea>

class Test2 : public QWidget
{
public:
Test2(QWidget* parent);
QScrollArea* t;
QSize sizeHint() const;
QSize s;
};

class Test : public QWidget
{
public:
Test();

private:
QVBoxLayout* vBoxLayout;
Test2* t2;
QScrollArea* t;
};


#endif // TEST_H

Doesn't work:


#include <QtGui>
#include "test.h"

Test::Test()
{
vBoxLayout = new QVBoxLayout();
t2 = new Test2(this);
//t = new QScrollArea();
//t->setWidget(t2);
vBoxLayout->addWidget(t2);
setLayout(vBoxLayout);
}

int main(int argc, char **argv)
{
QApplication app(argc, argv);
Test* t = new Test();
t->show();
return app.exec();
}

Test2::Test2(QWidget* parent) : QWidget(parent)
{
s.setWidth(200);
s.setHeight(200);
t = new QScrollArea();
t->setWidget(this);
}

QSize Test2::sizeHint() const
{
return s;
}

test.h remains unmodified.

Diph
25th April 2010, 16:06
Maybe you should pass t2->scrollArea to vBoxLayout instead of vBoxLayout->addWidget(t2)

E: Widget is a child of scrollArea...

planetLars
25th April 2010, 16:34
You are right, yes.

Thank you!

wysota
25th April 2010, 16:35
Change the "widgetResizable" property of the scroll area to true. Also bear in mind your construction is very weird (or at least unusual). You allow test2 to be created with a parent and then you place it inside some other widget (the scroll area) that reparents the widget to itself. Furthermore there is no reference to the window (scroll area) outside the test2 object which leads to a certain memory leak.

planetLars
25th April 2010, 16:47
Diph already pointed out the source of error: I forgot that I don't get the QScrollArea fro free when I have it inside a Widget, I need to add the QScrollArea and not the Widget to the layout.

I have Destructors which delete every object created including the scroll area from the test2 destructor equivalent in my non-test code. Or is there something else about memory leaks I should be aware of?

Lars

wysota
25th April 2010, 19:43
I have Destructors which delete every object created including the scroll area from the test2 destructor equivalent in my non-test code. Or is there something else about memory leaks I should be aware of?

So when exactly do you delete the scroll area object? Actually I see the question is meaningless as you didn't want the scroll area to be a top level widget, despite what your code says...

planetLars
25th April 2010, 21:32
I delete the scrollbar when the class2-equivalent gets destroyed. And class2 gets deleted when the main application gets destroyed. I wanted my class2-equivalent be a self contained widget which (I can place anywhere and which) brings its own scroll area without the user having to fuss with scrollbars.

wysota
25th April 2010, 22:13
Don't you think it should inherit QScrollArea or QAbstractScrollArea then?

planetLars
25th April 2010, 22:59
This was a question. I ponder it further. Thanks. Good Night.

Lars