PDA

View Full Version : How do I conveniently insert a QScrollArea into a pre-designed UI?



ZB
19th September 2006, 13:54
I have an interface built with the designer in which I have a QLabel. This QLabel will resize with the interface and be used to display images that are sometimes larger than the actual QLabel, and thus I would like to fit it into a QScrollArea that will display scroll bars when necessary. Programmatically, I created the QScrollArea and tried adding it to the layout it was to be placed within, but could not manage to position nor size it without distorting the entire layout.

What I am wondering is, Is there any way that I could conveniently, or even automatically, have my QScrollArea adopt position, size, layout etc. of my QLabel, to make it contain, fit and size as intented?

Small and large version of my resizable interface to give you an idea of how it will position and resize, the selected widget is the QLabel:

http://hem.bredband.net/zeroblue/small.png

http://hem.bredband.net/zeroblue/large.png

munna
19th September 2006, 14:09
I think you need to have a QScrollArea in the place of your label and add the label to the scrollarea.

ZB
19th September 2006, 18:44
Thank you, I managed to fix my problem after alot of trial and error. I had to specify the whole stretch of rows and columns the scroll area was to cover before it would start to behave properly, rather than just its corner position.

wysota
20th September 2006, 02:06
Thank you, I managed to fix my problem after alot of trial and error. I had to specify the whole stretch of rows and columns the scroll area was to cover before it would start to behave properly, rather than just its corner position.

Maybe you should have set the minimum and maximum size for the containing frame (or the scrollarea itself) instead? At least if I understand your problem correctly...

ZB
20th September 2006, 13:45
Maybe you should have set the minimum and maximum size for the containing frame (or the scrollarea itself) instead? At least if I understand your problem correctly...

Thanks, the problem I had was fitting the QScrollArea inside the pre-built interface, some automatization for doing so would be nice, but I solved this by adding the scroll area to the grid layout and making it cover the same rows and columns as the QLabel did.

I have another problem, maybe you know how to solve this? The QLabel inside the QScrollArea displays an image (QPixmap) in natural size. When the QScrollArea is larger than the QLabel my image will move to the upper left corner, but how do I get the image to center itself inside the scroll area? Calling setAlignment(Qt::AlignCenter) on the QLabel has no effect, and I find no method in QScrollArea for centering contents.


EDIT: I tried having the QLabel scale to fit the QScrollArea but could not make the QPixmap retain its natural size and aspect ratio, having it scale is not an option. I also tried fitting an additional frame and layout inside QScrollArea to try centering my image but could not get it to work. Can Qt do this automatically for me or is there another manual solution that I have not found yet?

wysota
20th September 2006, 14:55
Did you try setting the label's alignment? You'll also have to enforce a minimum size on the label to fit the viewport of the scroll area.

jacek
20th September 2006, 15:10
I tried having the QLabel scale to fit the QScrollArea but could not make the QPixmap retain its natural size and aspect ratio, having it scale is not an option. I also tried fitting an additional frame and layout inside QScrollArea to try centering my image but could not get it to work. Can Qt do this automatically for me or is there another manual solution that I have not found yet?
All you need is two lines of code:


#include <QApplication>
#include <QLabel>
#include <QPixmap>
#include <QScrollArea>

class Test : public QScrollArea
{
public:
Test() : QScrollArea( 0 )
{
QLabel *l = new QLabel();
l->setAlignment( Qt::AlignCenter ); // <---
l->setPixmap( QPixmap( "b.jpg" ) );

setWidget( l );
setWidgetResizable( true ); // <---
}
};

int main( int argc, char **argv )
{
QApplication app( argc, argv );

Test t;
t.show();

return app.exec();
}

ZB
20th September 2006, 15:45
Thank you Wysota and Jacek, my bad I was using QLabel::setScaledContents instead of QScrollArea::setWidgetResizable. It works perfectly now.

Thanks again