Hello. I want to layer widgets in such a way that you can see through them and interact with all layers. The obvious thing seemed to be to subclass QStackedLayout and override the hiding behaviour. So I came up with this:

viewlayout.hpp:
Qt Code:
  1. class ViewLayout : public QStackedLayout {
  2. Q_OBJECT
  3. public:
  4. ViewLayout (QWidget * =0);
  5.  
  6. int addWidget (QWidget *);
  7. int insertWidget (int, QWidget *);
  8.  
  9. public slots:
  10. void setCurrentIndex (int);
  11. void setCurrentItem (QWidget *);
  12.  
  13. private:
  14. //! Shows everything in the widget.
  15. void show_all ();
  16. };
To copy to clipboard, switch view to plain text mode 
viewlayout.cpp:
Qt Code:
  1. ViewLayout :: ViewLayout (QWidget * parent) : QStackedLayout(parent) {
  2. }
  3.  
  4. int ViewLayout :: addWidget (QWidget * w) {
  5. int r = QStackedLayout :: addWidget (w);
  6. show_all ();
  7. return r;
  8. }
  9.  
  10. int ViewLayout :: insertWidget (int i, QWidget * w) {
  11. int r = QStackedLayout :: insertWidget (i, w);
  12. show_all ();
  13. return r;
  14. }
  15.  
  16. void ViewLayout :: setCurrentIndex (int) {
  17. // Do nothing. There is no notion of "current" item.
  18. }
  19.  
  20. void ViewLayout :: setCurrentItem (QWidget *) {
  21. // Do nothing. There is no notion of "current" item.
  22. }
  23.  
  24. void ViewLayout :: show_all () {
  25. for (int i=0; i<count(); i++) {
  26. itemAt(i)->widget()->raise();
  27. itemAt(i)->widget()->show();
  28. }
  29. }
To copy to clipboard, switch view to plain text mode 

The widget that uses it:
view.hpp:
Qt Code:
  1. class View : public QWidget {
  2. Q_OBJECT
  3. public:
  4. View (QWidget * =0);
  5.  
  6. //! Remove the window from the view but do not destroy it.
  7. void removeWidget (QWidget *);
  8.  
  9. //! \see QStackedLayout
  10. int addWidget (QWidget *);
  11.  
  12. //! \see QStackedLayout
  13. int insertWidget (int, QWidget *);
  14.  
  15. // \see QStackedLayout
  16. QWidget * widget (int) const;
  17. };
To copy to clipboard, switch view to plain text mode 

view.cpp:
Qt Code:
  1. View :: View (QWidget * parent) : QWidget(parent) {
  2. ViewLayout * l = new ViewLayout ();
  3. setLayout(l);
  4. }
  5.  
  6. void View :: removeWidget (QWidget * w) {
  7. layout() -> removeWidget (w);
  8. }
  9.  
  10. int View :: addWidget (QWidget * w) {
  11. ViewLayout * l = dynamic_cast<ViewLayout *>(layout());
  12. return l -> addWidget (w);
  13. }
  14.  
  15. int View :: insertWidget (int i, QWidget * w) {
  16. ViewLayout * l = dynamic_cast<ViewLayout *>(layout());
  17. return l -> insertWidget (i, w);
  18. }
  19.  
  20. QWidget * View :: widget (int i) const {
  21. ViewLayout * l = dynamic_cast<ViewLayout *>(layout());
  22. return l -> widget (i);
  23. }
To copy to clipboard, switch view to plain text mode 

And here's a demo window to put into such a stack.
bouncetest.hpp:
Qt Code:
  1. class BounceTest : public QFrame {
  2. Q_OBJECT
  3. public:
  4. BounceTest (QWidget * =0);
  5.  
  6. private:
  7. QWidget * obj;
  8. int vx;
  9. int vy;
  10.  
  11. private slots:
  12. void timeOut();
  13. };
To copy to clipboard, switch view to plain text mode 

bouncetest.cpp:
Qt Code:
  1. #include <stdlib.h>
  2.  
  3. namespace {unsigned int count = 0;}
  4.  
  5. BounceTest :: BounceTest (QWidget * parent) : QFrame(parent) {
  6. setFrameStyle(QFrame::Panel|QFrame::Plain);
  7. setLineWidth(3);
  8. QString s = "%1 %1 %1 %1\n%1 %1 %1 %1\n%1 %1 %1 %1\n%1 %1 %1 %1\n";
  9. obj = new QPushButton (s.arg(count++),this);
  10. obj->move(rand()%500,rand()%400);
  11. vx = 11;
  12. vy = 7;
  13. QTimer::singleShot(200,this,SLOT(timeOut()));
  14. }
  15.  
  16. void BounceTest :: timeOut () {
  17. int X = obj->x();
  18. int Y = obj->y();
  19. int W = width();
  20. int H = height();
  21. int Wo= obj->width();
  22. int Ho= obj->height();
  23. if ((X+Wo)>W || X<0)
  24. vx = -vx;
  25. if ((Y+Ho)>H || Y<0)
  26. vy = -vy;
  27. obj->move (X+vx,Y+vy);
  28. QTimer::singleShot(150,this,SLOT(timeOut()));
  29. }
To copy to clipboard, switch view to plain text mode 

And finally, main.cpp:
Qt Code:
  1. View * bt = new View ();
  2. bt->addWidget (new BounceTest());
  3. bt->addWidget (new BounceTest());
  4. bt->addWidget (new BounceTest());
  5. bt->addWidget (new BounceTest());
  6. bt->addWidget (new BounceTest());
  7. bt->resize(800,600);
  8. bt->show ();
To copy to clipboard, switch view to plain text mode 

At first glance, it seems okay except not all the Views have been resized.

Also, you can press the topmost button but none of the others. How do I rewrite BounceTest such that any click or mouse move not hitting any of its children wil propagate to the lower levels?

Thanks for reading this far.