Results 1 to 4 of 4

Thread: QScrollArea containing multiple widgets

  1. #1
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default QScrollArea containing multiple widgets

    I want a QScrollArea that contains multiple widgets but on top of each other (like a deck of cards):

    Qt Code:
    1. scrollArea->setWidget(widget1);
    To copy to clipboard, switch view to plain text mode 
    widget1 appears
    Qt Code:
    1. scrollArea->setWidget(widget2);
    To copy to clipboard, switch view to plain text mode 
    widget2 appears
    Qt Code:
    1. scrollArea->setWidget(widget1);
    To copy to clipboard, switch view to plain text mode 
    SEGMENTATION FAULT

    QScrollArea::setWidget(QWidget*) deletes the old widget before setting a new one, thus causing a segmentation fault. I've thought about keeping one widget and switching layouts, but Qt reference says:
    "If there already is a layout manager installed on this widget, QWidget won't let you install another. You must first delete the existing layout manager (returned by layout()) before you can call setLayout() with the new layout."
    meaning old layout would be deleted before setting a new one. I have my widgets stored in a QMap and I'd like to switch em during runtime. Switching layouts would work for me aswell, np. The only solution coming to my mind is copying widgets(layouts)before setWidget() call, but not sure how smart that is.

    Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QScrollArea containing multiple widgets

    There is of course MDI and QGraphicsView too. Both let you easily add widgets on top of each other and have scrolling capabilities.

    As for using a scrollArea. Why not add a single parent widget and use that parent widget to display the other widgets?
    You can use layouts anyway if you want to put them on top of each other.

  3. The following user says thank you to tbscope for this useful post:

    Mercurial (4th February 2011)

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QScrollArea containing multiple widgets

    I want a QScrollArea that contains multiple widgets but on top of each other (like a deck of cards)
    If you mean like a deck of cards where only the top card is visible at any time then look at QStackedWidget as the scrolled widget.

    If you mean a vertical list of widgets then you should combine a persistent QWidget with QVBoxLayout. You can add and remove widgets in the layout with addWidget(), insertWidget(), and removeWidget().

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

    Mercurial (4th February 2011)

  6. #4
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QScrollArea containing multiple widgets

    QStackedWidget solved my problems. Thanks alot.

Similar Threads

  1. Adding widgets to QScrollArea
    By ser_bur in forum Qt Programming
    Replies: 6
    Last Post: 19th August 2013, 10:38
  2. Multiple QTreeViews in a QScrollArea?
    By MarkSimon in forum Qt Programming
    Replies: 12
    Last Post: 23rd November 2010, 03:06
  3. attach more widgets to a QScrollArea
    By franco.amato in forum Qt Programming
    Replies: 39
    Last Post: 19th January 2010, 16:47
  4. QScrollArea With Custom Widgets
    By VireX in forum Qt Programming
    Replies: 30
    Last Post: 22nd April 2007, 16:48
  5. How to paint across/over multiple widgets?
    By richy in forum Qt Programming
    Replies: 3
    Last Post: 31st August 2006, 10:57

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.