Results 1 to 8 of 8

Thread: How do I conveniently insert a QScrollArea into a pre-designed UI?

  1. #1
    Join Date
    Sep 2006
    Posts
    19
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default How do I conveniently insert a QScrollArea into a pre-designed UI?

    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:




  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do I conveniently insert a QScrollArea into a pre-designed UI?

    I think you need to have a QScrollArea in the place of your label and add the label to the scrollarea.

  3. #3
    Join Date
    Sep 2006
    Posts
    19
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How do I conveniently insert a QScrollArea into a pre-designed UI?

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How do I conveniently insert a QScrollArea into a pre-designed UI?

    Quote Originally Posted by ZB View Post
    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...

  5. #5
    Join Date
    Sep 2006
    Posts
    19
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How do I conveniently insert a QScrollArea into a pre-designed UI?

    Quote Originally Posted by wysota View Post
    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?
    Last edited by ZB; 20th September 2006 at 13:44.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How do I conveniently insert a QScrollArea into a pre-designed UI?

    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.

  7. The following user says thank you to wysota for this useful post:

    ZB (20th September 2006)

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do I conveniently insert a QScrollArea into a pre-designed UI?

    Quote Originally Posted by ZB View Post
    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:
    Qt Code:
    1. #include <QApplication>
    2. #include <QLabel>
    3. #include <QPixmap>
    4. #include <QScrollArea>
    5.  
    6. class Test : public QScrollArea
    7. {
    8. public:
    9. Test() : QScrollArea( 0 )
    10. {
    11. QLabel *l = new QLabel();
    12. l->setAlignment( Qt::AlignCenter ); // <---
    13. l->setPixmap( QPixmap( "b.jpg" ) );
    14.  
    15. setWidget( l );
    16. setWidgetResizable( true ); // <---
    17. }
    18. };
    19.  
    20. int main( int argc, char **argv )
    21. {
    22. QApplication app( argc, argv );
    23.  
    24. Test t;
    25. t.show();
    26.  
    27. return app.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to jacek for this useful post:

    ZB (20th September 2006)

  10. #8
    Join Date
    Sep 2006
    Posts
    19
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How do I conveniently insert a QScrollArea into a pre-designed UI?

    Thank you Wysota and Jacek, my bad I was using QLabel::setScaledContents instead of QScrollArea::setWidgetResizable. It works perfectly now.

    Thanks again

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.