Results 1 to 5 of 5

Thread: Reuse a Qwidget with a QStackWidget

  1. #1
    ucomesdag Guest

    Question Reuse a Qwidget with a QStackWidget

    Hi,

    I'm looking for away to reuse a QWidget and add it multiple times to a QStackWidget.

    I need something close to this
    Qt Code:
    1. QGridLayout *myLayout = new QGridLayout;
    2.  
    3. QWidget *tmpWidget = new QWidget;
    4. tmpWidget->setLayout(myLayout);
    5.  
    6. QStackWidget *myStackWidget = new QStackWidget;
    7.  
    8. for(int i=0;i<10;++i)
    9. {
    10. myStackWidget->insertWidget(i, tmpWidget);
    11. };
    To copy to clipboard, switch view to plain text mode 
    Any help is appreciated, thanks.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reuse a Qwidget with a QStackWidget

    This is not possible... A widget cannot have more than one parent ( the stacked widgets page reparents the widget bas soon as you add it ).


    Do you need exactly the same instance of the widget in all pages? Then when you change the stacked widget page, just remove the tmpWidget from the previous position and add it to the current position. Not very nice, but...

    Ultimately, create 10 widgets, and that's it. If you want the changes from one widget to reflect in the others, just synchronize them with signals/slots.

    Regards

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Reuse a Qwidget with a QStackWidget

    if you need to show the same data on various views, you might want to design it with the model/view approach.
    http://doc.trolltech.com/4.2/model-v...ogramming.html
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  4. #4
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Reuse a Qwidget with a QStackWidget

    That or you could make that widget contain static variables (so they all have the same data) and create a new instance for each page of your stacked widget. That should also free you up to make some parts of your widget different on each page.
    Life without passion is death in disguise

  5. #5
    ucomesdag Guest

    Default Re: Reuse a Qwidget with a QStackWidget

    Is there else a way to duplicate the widget?

    Found the answer myself: No...
    Last edited by ucomesdag; 25th April 2007 at 03:18.

Similar Threads

  1. Replies: 4
    Last Post: 24th April 2007, 13:18
  2. Replies: 3
    Last Post: 8th March 2007, 14:54
  3. Showing QMainWindow without showing a child QWidget
    By discostu in forum Qt Programming
    Replies: 3
    Last Post: 4th March 2007, 09:03
  4. Replies: 1
    Last Post: 2nd May 2006, 21:11
  5. QScrollArea problem positioning a QWidget inside
    By Spectator in forum Qt Programming
    Replies: 4
    Last Post: 20th February 2006, 22:59

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.