parentWidget.findChildren<QWidget *>()
bool MyAutoRaiseWidget::eventFilter(QObject *obj, QEvent *event){ if ( event->type() == QEvent::MouseButtonPress ) raise(); return QWidget::eventFilter(obj, event);}
C++ (Qt)#include <QApplication>#include <QMainWindow>#include <QMenuBar>#include <QGridLayout>#include <QToolBar>#include <QToolButton>#include <QGraphicsDropShadowEffect> template< typename T > T findParent( QObject* o ){ while( o ) { if( T po = /*qobject_cast*/ dynamic_cast< T >( o ) ) return po; o = o->parent(); } return 0;} class SubWindow : public QMainWindow{public: SubWindow( QWidget* p = 0 ) : QMainWindow( p ) { setWindowFlags( windowFlags() & ~Qt::Window ); setAutoFillBackground( true ); QGraphicsDropShadowEffect* shadow = new QGraphicsDropShadowEffect( this ); shadow->setColor( QColor( 0, 0, 0, 255 ) ); shadow->setOffset( QPointF( 0, 0 ) ); setGraphicsEffect( shadow ); shadow->setBlurRadius( 16 ); qApp->installEventFilter( this ); }protected: bool eventFilter( QObject* o, QEvent* e ) { if( e->type() == QEvent::MouseButtonPress && o->isWidgetType() ) { if( findParent< SubWindow* >( o ) == this ) raise(); } return QMainWindow::eventFilter( o, e ); }}; class MainWin : public SubWindow{public: MainWin( QWidget* p = 0 ) : SubWindow( p ) { QMenuBar* mbar = menuBar(); mbar->addMenu( "&File" ); mbar->addMenu( "&View" ); mbar->addMenu( "&Help" ); QToolBar* tbar = addToolBar( "" ); for( int i = 0; i < 5; i++ ) tbar->addAction( QString::number( i + 1 ) ); QWidget* bw = new QWidget; QGridLayout* l = new QGridLayout( bw ); for( int r = 0; r < 5; r++ ) for( int c = 0; c < 5; c++ ) { QToolButton* tb = new QToolButton; tb->setText( QString( "%1:%2" ).arg( r ).arg( c ) ); l->addWidget( tb, r, c ); } setCentralWidget( bw ); }}; int main( int argc, char *argv[] ){ QApplication a( argc, argv ); QWidget w; QSize sz; for( int i = 0; i < 3; i++ ) { MainWin* mw = new MainWin( &w ); mw->adjustSize(); mw->move( i * 100 + 20, i * 100 + 20 ); sz = sz.expandedTo( mw->size() ); } w.resize( sz.width() * 2 + 20 , sz.height() * 2 + 20 ); w.show(); return a.exec();}
C++ (Qt) qApp->installEventFilter( this );