Results 1 to 4 of 4

Thread: Trying to add new widgets to a scroll area on click

  1. #1
    Join Date
    Apr 2019
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Question Trying to add new widgets to a scroll area on click

    I am trying to add a custom widget to a scroll area on click.

    Let say this is my custom widget
    2.jpg

    And this is the main window where I can add my custom widget one by one when I click the "add" button.
    3.jpg

    Now when I try to add many custom widgets to that scroll area, the scroll bar didn't show up but instead
    all the custom widgets huddled within the scroll area.


    Clicked twice the "add" button:
    4.jpg

    Clicked the "add" button for 5 times:
    5.jpg

    But it works when I am adding many QPushButtons:
    6.jpg


    I am new to Qt so I hope that someone can show me how to do this in ui. Thanks in advance.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,976
    Thanks
    232
    Thanked 627 Times in 617 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Trying to add new widgets to a scroll area on click

    I assume that you have implemented your QScrollArea so that it contains a QWidget as the widget it controls, and that widget contains a QVBoxLayout that you are adding the copies of your custom widget to. If not, that's first thing you need to fix. Read the documentation for QScrollArea::setWidget() so you do this correctly.

    You also need to call QScrollArea::setWidgetResizable() with "true".

    As you add each copy of your custom widget, you may want to call QScrollArea::ensureWidgetVisible() with the pointer to that custom widget so the scroll area will scroll so it can be seen.

    Calling QScrollArea::setWidgetResizable() with true is supposed to automatically resize the QWidget as its contents grow, but if that doesn't work in your case, you may need to resize the QWidget manually as you add custom widgets to make sure it grows to contain them. Otherwise, you will get the behavior you see - the custom widgets get squashed because the QWidget does not resize to fit them. As the QWidget grows to be bigger than the scroll area, the scroll bars will automatically appear. When you compute the size of the QWidget, be sure to take into account the spacing and margins of the layout and size and margins of your custom widget, and not just the size of the custom widget alone. If you don't do this, the custom widgets will still get squashed, just not as quickly.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Apr 2019
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Trying to add new widgets to a scroll area on click

    This is what the object inspector currently looks like
    7.PNG

    in form ui
    8.jpg

    I put the scroll area at the second page of the stacked widget. On this page, there is a little vertical layout on the top left corner and a scroll area at the right side. I made a function for that "add" function.

    Qt Code:
    1. void MainWindow::on_pushButton_2_clicked(){
    2.  
    3. ui->scrollArea->setWidgetResizable(true);
    4.  
    5. ui->scrollAreaWidgetContents->setLayout(layout);
    6.  
    7. ui->scrollArea->ensureWidgetVisible(ui->scrollAreaWidgetContents);
    8.  
    9. // to add my custome widget
    10. ui->scrollAreaWidgetContents->layout()->addWidget(new modal(this));
    11.  
    12. ui->scrollArea->ensureWidgetVisible(ui->scrollAreaWidgetContents);
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

    "QScrollArea so that it contains a QWidget as the widget it controls, and that widget contains a QVBoxLayout "

    I'm not sure what QWidget should this be, but I assume that it is the scrollAreaWidgetContent.

    So I wrote the line " ui->scrollAreaWidgetContents->setLayout(layout); " as you mentioned "and that widget contains a QVBoxLayout that you are adding the copies of your custom widget to"

    and finally, I wrote the line to invoke ensureWidgetVisible(). But the widgets still got squashed.

    When I run the program it says "QLayout: Attempting to add QLayout "" to MainWindow "MainWindow", which already has a layout". I assume this is because I tried to put 2 layouts on the same page of the stackedWidget?

    I am not sure if I have to call all those items via stackedWidget such as "ui->stackedWidget->page_2->scrollArea->...."

    I need more help from you. Thanks in advance.
    Attached Images Attached Images
    • File Type: png 7.PNG (15.6 KB, 3 views)
    Last edited by EnzoCheng97; 27th April 2019 at 05:04.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,976
    Thanks
    232
    Thanked 627 Times in 617 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Trying to add new widgets to a scroll area on click

    From looking at your widget hierarchy, it seems like page2 of your stacked layout is not correct. That whole widget needs a layout so that the set of radio buttons and so forth is in one part of it, and the scroll area in the other part. You probably want a QHBoxLayout for this. When you finish, your page2 widget should look something like this:

    Qt Code:
    1. QWidget (page_2)
    2. ---- QVBoxLayout (left hand side of hbox)
    3. ------- QCheckBox (checkBox)
    4. ------- QPushButton (pushButton_2
    5. ------- etc.
    6. ---- QScrollArea (right hand side of hbox)
    7. ------ QWidget (scrollAreaWidgetContents)
    8. -------- QVBoxLayout (layout for scroll area's widget)
    9. ---------- your custom widget #1
    10. ---------- your custom widget #2
    11. ---------- etc.
    To copy to clipboard, switch view to plain text mode 

    Your page 1 widget also needs a layout. Any time you have child widgets within another QWidget, you need to place a layout of some kind inside that QWidget to manage the layout of the child widgets. It is bad practice to simply add child widgets directly to a QWidget with absolute positions. By using a layout, the child widgets will behave properly as the window is resized. With no layout, the child widgets won't move or resize along with the widget that contains them.

    You also do not need the QWidget you have added as the central widget for the main window. You can make the QStackedWidget the central widget directly.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Scroll bars in scroll area not comming in scroll area
    By aurora in forum Qt Programming
    Replies: 3
    Last Post: 4th January 2012, 06:50
  2. Scroll bars in scroll area not comming
    By aurora in forum Qt Programming
    Replies: 3
    Last Post: 27th December 2011, 19:56
  3. Q Scroll Area that scrolls??
    By hakermania in forum Newbie
    Replies: 6
    Last Post: 24th August 2010, 19:48
  4. Scroll Area problem
    By Lodhart in forum Qt Programming
    Replies: 0
    Last Post: 10th October 2009, 13:03
  5. abstract scroll area
    By moowy in forum Qt Programming
    Replies: 2
    Last Post: 2nd October 2006, 09:15

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.