Results 1 to 4 of 4

Thread: QScrollArea inner widget problem

  1. #1
    Join Date
    Jan 2006
    Posts
    5
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default QScrollArea inner widget problem

    I need QScrollArea to contains some images

    Qt Code:
    1. TestScrollArea::TestScrollArea(QWidget *parent, Qt::WFlags flags)
    2. : QMainWindow(parent, flags)
    3. {
    4. ui.setupUi(this);
    5.  
    6. QWidget* widget = new QWidget;
    7. QVBoxLayout* layout = new QVBoxLayout( widget);
    8.  
    9. QImage image("D:\\data\\photo\\me\\bg2009.jpg");
    10. Q_ASSERT( image.isNull() == false);
    11.  
    12. QLabel* label1 = new QLabel;
    13. label1->setPixmap( QPixmap::fromImage( image));
    14. layout->addWidget( label1);
    15.  
    16. ui.scrollArea->setBackgroundRole(QPalette::Dark);
    17. ui.scrollArea->setWidget( widget);
    18. }
    To copy to clipboard, switch view to plain text mode 

    This code was placed to the main window constructor and it worked ok (image displayed). Then I created QScrollArea child class and moved the code to its contructor:

    Qt Code:
    1. TestImageViewer::TestImageViewer(QWidget *parent)
    2. : QScrollArea(parent)
    3. {
    4. QWidget* widget = new QWidget;
    5. QVBoxLayout* layout = new QVBoxLayout( widget);
    6.  
    7. QImage image("D:\\data\\photo\\me\\bg2009.jpg");
    8. Q_ASSERT( image.isNull() == false);
    9.  
    10. QLabel* label1 = new QLabel;
    11. label1->setPixmap( QPixmap::fromImage( image));
    12. layout->addWidget( label1);
    13.  
    14. setBackgroundRole(QPalette::Dark);
    15. setWidget( widget);
    16. }
    To copy to clipboard, switch view to plain text mode 

    Now I see nothing except a grey rectangle. What I'm doing wrong?

  2. #2
    Join Date
    Apr 2009
    Location
    Italy
    Posts
    70
    Thanks
    23
    Thanked 15 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QScrollArea inner widget problem

    The way you create your layout is odd, why are you passing the widget as parent? Usually, you want to create the layout with no parent and then call:
    Qt Code:
    1. widget->setLayout(layout);
    To copy to clipboard, switch view to plain text mode 
    See code here:
    http://doc.trolltech.com/4.6/qvboxlayout.html#details

    edit: oops, I just read you can pass the widget as parent. However, setLayout() is worth a try :-)
    Last edited by mattc; 19th January 2010 at 16:39.

  3. #3
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QScrollArea inner widget problem

    Quote Originally Posted by agaf View Post
    Now I see nothing except a grey rectangle. What I'm doing wrong?
    Just a possibility:

    Are you sure that when you created the TestImageViewer class, you made the corresponding changes (presumably in Qt Designer) so that ui.setupUi is constructing a TestImageViewer where it used to construct a plain QScrollArea?

  4. #4
    Join Date
    Jan 2006
    Posts
    5
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QScrollArea inner widget problem

    Quote Originally Posted by Coises View Post
    Just a possibility:

    Are you sure that when you created the TestImageViewer class, you made the corresponding changes (presumably in Qt Designer) so that ui.setupUi is constructing a TestImageViewer where it used to construct a plain QScrollArea?
    Yes, that was I checked first. To say the truth in the first case "empty" TestImageViewer is used as well

Similar Threads

  1. QScrollArea: clearing the layout of the inner widget
    By mattc in forum Qt Programming
    Replies: 1
    Last Post: 3rd August 2009, 08:09
  2. resizing widget inside QScrollArea
    By mastupristi in forum Newbie
    Replies: 4
    Last Post: 16th July 2009, 19:29
  3. Simple QScrollArea problem
    By jmsbc in forum Qt Programming
    Replies: 4
    Last Post: 2nd December 2008, 18:45
  4. Replies: 3
    Last Post: 14th April 2006, 11:33
  5. [qt4] QScrollArea corner widget?
    By sertrem in forum Qt Programming
    Replies: 2
    Last Post: 12th January 2006, 21:42

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.