Results 1 to 9 of 9

Thread: Adding QStackedWidget to QScrollArea

  1. #1
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Adding QStackedWidget to QScrollArea

    Hi Everyone,

    I have 2 QWidget classes in my Project by name Widget and TestWidget. In my Widget Class I have the following objects declared-

    widget.h
    Qt Code:
    1. TestWidget* objTest;
    2. QScrollArea scrollArea;
    3. QStackedWidget stackedWd;
    To copy to clipboard, switch view to plain text mode 

    and in widget.cpp
    Qt Code:
    1. objTest = new TestWidget;
    2.  
    3. scrollArea.setParent(this);
    4. scrollArea.setGeometry(20,20,360,100);
    5.  
    6. scrollArea.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    7. scrollArea.setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    8. scrollArea.setWidget(&stackedWd);
    9. //scrollArea.setWidget(objTest);
    10.  
    11. stackedWd.addWidget(objTest);
    12. stackedWd.setCurrentWidget(objTest);
    13. objTest->raise();
    14. objTest->activateWindow();
    To copy to clipboard, switch view to plain text mode 

    But unfortunately nothing is visible in the QScrollArea. On the contrary if I exclude QStackedWidget then TestWidget appears fine in the ScrollArea along with the scroll, following is the code for it-

    widget.cpp
    Qt Code:
    1. objTest = new TestWidget;
    2.  
    3. scrollArea.setParent(this);
    4. scrollArea.setGeometry(20,20,360,100);
    5.  
    6. scrollArea.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    7. scrollArea.setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    8. scrollArea.setWidget(objTest);
    To copy to clipboard, switch view to plain text mode 

    I am just not able to understand why the same thing doesn't happen when I use a QStackedWidget as an intermediate. It would be great if someone would guide me regarding this.

    For more info TestWidget is a simple QWidget class with just a QLabel in it. The size of TestWidget is 320x240.

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

    Default Re: Adding QStackedWidget to QScrollArea

    You are not using layouts, as far as I can tell, which may indicate that the stacked widget size is 0. Keeping widget objects as members of another widget class is also not the best idea, you should keep pointers instead and allocate objects in constructor of the parent widget.
    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
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Adding QStackedWidget to QScrollArea

    Quote Originally Posted by wysota View Post
    You are not using layouts, as far as I can tell, which may indicate that the stacked widget size is 0.
    Where should I put the Layout? I mean I should add the ScrollArea to the GridLayout or the StackedWidget to the GridLayout?
    Quote Originally Posted by wysota View Post
    Keeping widget objects as members of another widget class is also not the best idea, you should keep pointers instead and allocate objects in constructor of the parent widget.
    I have taken a pointer only naa? And the particular code is inside the constructor of Widget class, to put the full code-
    Qt Code:
    1. Widget::Widget(QWidget *parent) :
    2. QWidget(parent),
    3. ui(new Ui::Widget)
    4. {
    5. ui->setupUi(this);
    6. objTest = new TestWidget;
    7.  
    8. scrollArea.setParent(this);
    9. scrollArea.setGeometry(20,20,360,100);
    10.  
    11. scrollArea.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    12. scrollArea.setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    13. scrollArea.setWidget(&stackedWd);
    14. //scrollArea.setWidget(objTest);
    15.  
    16. stackedWd.addWidget(objTest);
    17. stackedWd.setCurrentWidget(objTest);
    18. objTest->raise();
    19. objTest->activateWindow();
    20. }
    To copy to clipboard, switch view to plain text mode 
    By the way, why is it a bad idea?

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

    Default Re: Adding QStackedWidget to QScrollArea

    Quote Originally Posted by sattu View Post
    Where should I put the Layout? I mean I should add the ScrollArea to the GridLayout or the StackedWidget to the GridLayout?
    I don't see any GridLayout here. You should apply a layout to your "Widget" instance instead of using setGeometry, etc.

    Qt Code:
    1. Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) {
    2. QVBoxLayout *l = new QVBoxLayout(this); // <-- create layout for the widget
    3. scrollArea = new QScrollArea(this);
    4. l->addWidget(scrollArea); // <-- put scroll area into the layout
    5. stackedWd = new QStackedWidget(scrollArea);
    6. scrollArea->setWidget(stackedWd);
    7. scrollArea->setWidgetResizable(true); // <-- allow the scroll area to manage its client widget size
    8. }
    To copy to clipboard, switch view to plain text mode 

    I have taken a pointer only naa?
    Naa

    And the particular code is inside the constructor of Widget class, to put the full code-
    Qt Code:
    1. scrollArea.setParent(this);
    To copy to clipboard, switch view to plain text mode 
    The dot indicates this is not a pointer.

    By the way, why is it a bad idea?
    Because parents free their child widgets when they are themselves deleted and compiler will try to delete the same object when its scope ends resulting in a possible double-free error.
    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.


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

    sattu (31st July 2014)

  6. #5
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Adding QStackedWidget to QScrollArea

    Quote Originally Posted by wysota View Post
    I don't see any GridLayout here. You should apply a layout to your "Widget" instance instead of using setGeometry, etc.
    Thanks a lot, first step achieved . Now the TestWidget is visible in the ScrollArea. But the scroll doesn't appear, do I have to enable any other option along with Resizable factor?

    Quote Originally Posted by wysota View Post
    Naa
    I thought you were speaking of TestWidget class.

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

    Default Re: Adding QStackedWidget to QScrollArea

    If you make the client widget resizable then it will be resized to the scroll area If you want scroll bars to appear, you have to enforce a size on the client widget so that it doesn't fit the scroll area and scroll bars will appear. So basically the client widget has to tell that it cannot be smaller than X but it can be larger than that. If X is lower than the scroll area size then scroll area will enlarge the client to its own size. If X is larger than the scroll area, then scroll bars will appear.
    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
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Adding QStackedWidget to QScrollArea

    Quote Originally Posted by wysota View Post
    If you make the client widget resizable then it will be resized to the scroll area If you want scroll bars to appear, you have to enforce a size on the client widget so that it doesn't fit the scroll area and scroll bars will appear. So basically the client widget has to tell that it cannot be smaller than X but it can be larger than that. If X is lower than the scroll area size then scroll area will enlarge the client to its own size. If X is larger than the scroll area, then scroll bars will appear.
    Thanks a lot wysota. It took me some time to understand the Minimum Size concept and what should be it's idle value. I am setting the minimum size of the StackedWidget as follows-
    Qt Code:
    1. stackedWd->setMinimumSize(objTest->width(),objTest->height());
    To copy to clipboard, switch view to plain text mode 
    I hope I understood it correct.

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

    Default Re: Adding QStackedWidget to QScrollArea

    Qt Code:
    1. stackedWd->setMinimumSize(objTest->width(),objTest->height());
    To copy to clipboard, switch view to plain text mode 
    Well... This does not basically make much sense. You should have a layout there as well and the layout should be responsible for managing the size. Your first step should really be to start using layouts everywhere instead of manual setGeometry calls.
    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. The following user says thank you to wysota for this useful post:

    sattu (31st July 2014)

  11. #9
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Adding QStackedWidget to QScrollArea

    Quote Originally Posted by wysota View Post
    Well... This does not basically make much sense. You should have a layout there as well and the layout should be responsible for managing the size. Your first step should really be to start using layouts everywhere instead of manual setGeometry calls.
    Okies, I am going to explore that next. Just for the sake of trying I used a GridLayout in my TestWidget class, as a result of which I am able to get the scrollBar without having to manually call the Geometry functions, as mentioned by you.
    But then the spacing between the Child Widgets (of the GridLayout) change when I run the program compared to how it appears in the .ui file (I used drag and drop for a quick trial). So I will get back once I have read the documentation and gone through some Layout examples.

Similar Threads

  1. Adding widgets to QScrollArea
    By ser_bur in forum Qt Programming
    Replies: 6
    Last Post: 19th August 2013, 10:38
  2. QStackedWidget
    By sattu in forum Qt Programming
    Replies: 6
    Last Post: 28th September 2011, 13:44
  3. Replies: 1
    Last Post: 3rd December 2010, 13:02
  4. Help with QStackedWidget
    By onírico in forum Newbie
    Replies: 6
    Last Post: 12th November 2009, 16:34
  5. Replies: 2
    Last Post: 10th March 2008, 20:16

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.