Results 1 to 11 of 11

Thread: QScrollArea ->setWidget(this) doesn't produce a ScrollArea. Why?

  1. #1
    Join Date
    Apr 2010
    Posts
    25
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Question QScrollArea ->setWidget(this) doesn't produce a ScrollArea. Why?

    Hello,

    putting a Widget inside a QScrollArea despends on where the pointer variable to the QScrollArea is saved. If I have: (Pseudo-Code)

    Qt Code:
    1. class1():new class2(&class1)
    2.  
    3. class2():QScrollArea->setWidget(this)
    To copy to clipboard, switch view to plain text mode 

    I don't get scrollbars.

    If I have:

    Qt Code:
    1. class1():new class2(&class1),QScrollArea->setWidget(class2)
    To copy to clipboard, switch view to plain text mode 

    I get scrollbars.

    Why is this?

    The only differences is the location of the QScrollArea pointer variable (class1 or class 2). In both cases class2 gets set and class2 should be reparented from class1 to QScrollArea while class1 becomes the parent of QScrollArea.
    As an object shouldn't have access to the scope it is placed in, it shouldn't matter whether the pointer variable is in class2 or in class1!

    BR

    Lars

  2. #2
    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: QScrollArea ->setWidget(this) doesn't produce a ScrollArea. Why?

    Could you provide real code instead of this pseudo-pseudo-code?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2010
    Posts
    25
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QScrollArea ->setWidget(this) doesn't produce a ScrollArea. Why?

    yes, here it is:

    works: test.cpp

    Qt Code:
    1. #include <QtGui>
    2. #include "test.h"
    3.  
    4. Test::Test()
    5. {
    6. vBoxLayout = new QVBoxLayout();
    7. t2 = new Test2(this);
    8. t = new QScrollArea();
    9. t->setWidget(t2);
    10. vBoxLayout->addWidget(t);
    11. setLayout(vBoxLayout);
    12. }
    13.  
    14. int main(int argc, char **argv)
    15. {
    16. QApplication app(argc, argv);
    17. Test* t = new Test();
    18. t->show();
    19. return app.exec();
    20. }
    21.  
    22. Test2::Test2()
    23. {
    24. s.setWidth(200);
    25. s.setHeight(200);
    26. //t = new QScrollArea();
    27. //t->setWidget(this);
    28. }
    29.  
    30. QSize Test2::sizeHint() const
    31. {
    32. return s;
    33. }
    To copy to clipboard, switch view to plain text mode 

    test.h:

    Qt Code:
    1. #ifndef TEST_H
    2. #define TEST_H
    3.  
    4.  
    5. #include <QWidget>
    6. #include <QVBoxLayout>
    7. #include <QScrollArea>
    8.  
    9. class Test2 : public QWidget
    10. {
    11. public:
    12. Test2(QWidget* parent);
    13. QSize sizeHint() const;
    14. QSize s;
    15. };
    16.  
    17. class Test : public QWidget
    18. {
    19. public:
    20. Test();
    21.  
    22. private:
    23. QVBoxLayout* vBoxLayout;
    24. Test2* t2;
    25. };
    26.  
    27.  
    28. #endif // TEST_H
    To copy to clipboard, switch view to plain text mode 

    Doesn't work:

    Qt Code:
    1. #include <QtGui>
    2. #include "test.h"
    3.  
    4. Test::Test()
    5. {
    6. vBoxLayout = new QVBoxLayout();
    7. t2 = new Test2(this);
    8. //t = new QScrollArea();
    9. //t->setWidget(t2);
    10. vBoxLayout->addWidget(t2);
    11. setLayout(vBoxLayout);
    12. }
    13.  
    14. int main(int argc, char **argv)
    15. {
    16. QApplication app(argc, argv);
    17. Test* t = new Test();
    18. t->show();
    19. return app.exec();
    20. }
    21.  
    22. Test2::Test2(QWidget* parent) : QWidget(parent)
    23. {
    24. s.setWidth(200);
    25. s.setHeight(200);
    26. t = new QScrollArea();
    27. t->setWidget(this);
    28. }
    29.  
    30. QSize Test2::sizeHint() const
    31. {
    32. return s;
    33. }
    To copy to clipboard, switch view to plain text mode 

    test.h remains unmodified.
    Last edited by planetLars; 25th April 2010 at 15:38. Reason: forgot submitting parent for class 2, still doesn't work though

  4. #4
    Join Date
    Jun 2008
    Posts
    24
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QScrollArea ->setWidget(this) doesn't produce a ScrollArea. Why?

    Maybe you should pass t2->scrollArea to vBoxLayout instead of vBoxLayout->addWidget(t2)

    E: Widget is a child of scrollArea...

  5. The following user says thank you to Diph for this useful post:

    planetLars (25th April 2010)

  6. #5
    Join Date
    Apr 2010
    Posts
    25
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QScrollArea ->setWidget(this) doesn't produce a ScrollArea. Why?

    You are right, yes.

    Thank you!

  7. #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: QScrollArea ->setWidget(this) doesn't produce a ScrollArea. Why?

    Change the "widgetResizable" property of the scroll area to true. Also bear in mind your construction is very weird (or at least unusual). You allow test2 to be created with a parent and then you place it inside some other widget (the scroll area) that reparents the widget to itself. Furthermore there is no reference to the window (scroll area) outside the test2 object which leads to a certain memory leak.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Apr 2010
    Posts
    25
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QScrollArea ->setWidget(this) doesn't produce a ScrollArea. Why?

    Diph already pointed out the source of error: I forgot that I don't get the QScrollArea fro free when I have it inside a Widget, I need to add the QScrollArea and not the Widget to the layout.

    I have Destructors which delete every object created including the scroll area from the test2 destructor equivalent in my non-test code. Or is there something else about memory leaks I should be aware of?

    Lars

  9. #8
    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: QScrollArea ->setWidget(this) doesn't produce a ScrollArea. Why?

    Quote Originally Posted by planetLars View Post
    I have Destructors which delete every object created including the scroll area from the test2 destructor equivalent in my non-test code. Or is there something else about memory leaks I should be aware of?
    So when exactly do you delete the scroll area object? Actually I see the question is meaningless as you didn't want the scroll area to be a top level widget, despite what your code says...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #9
    Join Date
    Apr 2010
    Posts
    25
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QScrollArea ->setWidget(this) doesn't produce a ScrollArea. Why?

    I delete the scrollbar when the class2-equivalent gets destroyed. And class2 gets deleted when the main application gets destroyed. I wanted my class2-equivalent be a self contained widget which (I can place anywhere and which) brings its own scroll area without the user having to fuss with scrollbars.

  11. #10
    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: QScrollArea ->setWidget(this) doesn't produce a ScrollArea. Why?

    Don't you think it should inherit QScrollArea or QAbstractScrollArea then?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #11
    Join Date
    Apr 2010
    Posts
    25
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QScrollArea ->setWidget(this) doesn't produce a ScrollArea. Why?

    This was a question. I ponder it further. Thanks. Good Night.

    Lars

Similar Threads

  1. Definitively QScrollArea doesn't want work. I need help.
    By franco.amato in forum Qt Programming
    Replies: 1
    Last Post: 7th January 2010, 07:21
  2. Replies: 2
    Last Post: 24th August 2009, 12:40
  3. why scrollbar doesn't display in QscrollArea
    By myhot21 in forum Qt Programming
    Replies: 2
    Last Post: 23rd July 2009, 02:38
  4. Replies: 1
    Last Post: 26th December 2008, 12:59
  5. How to dynamiclly produce the tab by QT/E?
    By xxthink in forum Qt Programming
    Replies: 1
    Last Post: 27th September 2008, 22:08

Tags for this Thread

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.