PDA

View Full Version : resizing widget inside QScrollArea



mastupristi
13th July 2009, 14:17
I have a QScrollArea, and I want the widget inside its viewPort to be resized to the maximum viewport size.
I have my class Dialog derived from QDialog and I have redefined resizeEvent() method, as follows:

#include <QVBoxLayout>

Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
sa = new QScrollArea(this);
sa->setMinimumSize(400,300);
sa->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
sa->setVerticalScrollBarPolicy ( Qt::ScrollBarAlwaysOn );
sa->setHorizontalScrollBarPolicy ( Qt::ScrollBarAlwaysOn );

qd = new QDial(this);
qd->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
sa->setWidget(qd);

QVBoxLayout *mainLayout = new QVBoxLayout();
mainLayout->addWidget(sa);

setLayout(mainLayout);
}

Dialog::~Dialog()
{
delete qd;
delete sa;
delete layout();
}

#include <cstdio>

void Dialog::resizeEvent ( QResizeEvent * event )
{
qd->resize(sa->viewport()->contentsRect().size());

QString str;
str = "sa->viewport()->contentsRect().size() " + QString::number(sa->viewport()->contentsRect().size().width()) + "," + QString::number(sa->viewport()->contentsRect().size().height());
printf("%s\n", str.toAscii().constData());
}

Once started I expect the QDial to be resized to viewPort size, but I obtain the following:
http://img32.imageshack.us/img32/9470/testqscrollarea.png
And on the console it is wrote:

sa->viewport()->contentsRect().size() 96,26
as if the QScrollArea was something like 100x30. But if I try to resize all works, so the piece of code in resizeEvent() works.

What is wrong? Where is the mistake?
I also attach the project

thanks

manishkyl
13th July 2009, 19:03
u are not helping the qdial to resize are u ?
qd->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

mastupristi
14th July 2009, 10:34
u are not helping the qdial to resize are u ?
qd->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
and u are not tring to resize the dialog manually aren't u?
Even if the sizePolicy is Fixed I can resize the qdial, infact resizing the dialog manually all works.
Instead when the application start, the first time the widgets are shown, the qdial is too small, and it seems that is the QScrollArea::viewPort to be very small.
Please try to compile and take a look at the console.
If you simply start and close the application without other interaction, you can see on the console

sa->viewport()->contentsRect().size() 96,26
then try to start and resize the dialog. What is written on console?

It seems that the first time I read sa->viewport()->contentsRect().size() the scrollarea is not yet resized to its final size

thanks

jpn
16th July 2009, 19:24
Doesn't QScrollArea::widgetResizable do what you want?

mastupristi
16th July 2009, 19:29
Doesn't QScrollArea::widgetResizable do what you want?
I don't know... the widget need to be resized so as a magnification only horizontally.

thanks