PDA

View Full Version : QScrollArea margins



munna
18th September 2006, 15:44
Hi,

I have a scroll area in which i need to palce a widget with some margin so that it looks as if it is placed at the center of the area.

Doing this was very simple using Qt3 but I am not able to find a way to do this using Qt4.

Can someone please help?

Thanks a lot.

high_flyer
18th September 2006, 15:50
did you try QScrollArea::ensureVisible ( int x, int y, int xmargin = 50, int ymargin = 50 )?

munna
18th September 2006, 16:02
did you try QScrollArea::ensureVisible ( int x, int y, int xmargin = 50, int ymargin = 50 )?

I thought that was to bring the point (x,y) int the scroll area appear in the viewport with the given margins.

Am I wrong?

high_flyer
18th September 2006, 16:21
you are right - but isn't it exactly what you want?

munna
18th September 2006, 16:26
No, I dont want that.

In Qt3, if I use :



scrollview->addChild(widget,10,10);


The widget will appear from the point (10,10), which means I will have some top and left margin.

Using resizeContents() I can get the right and the bottom margins also.

This is not so simple in Qt4.

wysota
18th September 2006, 16:35
You can add a layout to the area and add the widget to the layout. You can then set margins used by that layout and it should do what you want.

sunil.thaha
18th September 2006, 16:46
From the Book: Practical Qt
try adding the line:


scrollView->setResizePolicy( QScrollView::AutoOneFit );

munna
18th September 2006, 16:46
I tried




QVBoxLayout *mainLayout = new QVBoxLayout(scrollarea);
mainLayout->addWidget(widget);
scrollarea->setLayout(layout)
scrollarea->setWidget(widget)



but is not working. I am sure this is not what you meant. Can you please explain ?

Thanks.

wysota
18th September 2006, 17:36
It should probably be

QVBoxLayout *mainLayout = new QVBoxLayout(scrollarea->viewport());
mainLayout->addWidget(widget);
scrollarea->setLayout(layout);
If this doesn't help try:

QWidget *wgt = new QWidget();
QVBoxLayout *mainLayout = new QVBoxLayout();
mainLayout->addWidget(widget);
wgt->setLayout(mainLayout);
scrollarea->setWidget(wgt);

munna
18th September 2006, 18:07
No. Its not working.

I think I have to use some round about way to do this.

wysota
18th September 2006, 18:44
But what do you mean "it is not working"? Could you elaborate? Did you set the margin for that layout?

munna
18th September 2006, 19:20
Here is my code




previewArea = new PreviewArea(this);
previewArea->setWidgetResizable(true);

previewWidget = new PreviewWidget(previewArea);

QVBoxLayout *pLay = new QVBoxLayout(previewArea->viewport());
pLay->setMargin(10);
pLay->addWidget(previewWidget);

previewArea->setLayout(pLay);
previewArea->setWidget(previewWidget);



With the above code, when I close the window that contains the area, my application crashes. I think the crash has some thing to do with QVBoxLayout *pLay = new QVBoxLayout(previewArea->viewport());

The next option




previewArea = new PreviewArea(this);
previewArea->setWidgetResizable(true);
previewWidget = new PreviewWidget(onlyCurr,cd,previewArea);

QVBoxLayout *pLay = new QVBoxLayout();
pLay->setMargin(10);
pLay->addWidget(previewWidget);
previewArea->setLayout(pLay);
previewArea->setWidget(previewWidget);



The above code is also not working. But this is not crashing the application.

Thanks for your help.

wysota
18th September 2006, 19:56
Here is my code and it works:


#include <QApplication>
#include <QLabel>
#include <QScrollArea>
#include <QVBoxLayout>


int main(int argc, char **argv){
QApplication app(argc, argv);
QScrollArea sa;
QWidget *widget = new QWidget;
QVBoxLayout *lay = new QVBoxLayout;
QLabel *label = new QLabel("Some text");
label->setFrameShape(QFrame::Box);
lay->addWidget(label);
lay->setMargin(20);
widget->setLayout(lay);
sa.setWidget(widget);
sa.show();
return app.exec();
}