C++ (Qt)QLayout* getLayout( QWidget* w ){ while( w ) { if( w->layout() ) return w->layout(); w = w->parentWidget(); } return 0;}
C++ (Qt)QLayout *layout = qobject_cast<QLayout*>( widget->parent() );if( !layout ) qDebug() << "Виджет не расположен в layout"; qDebug() << layout->objectName();
C++ (Qt) qDebug() << button1->parent()->objectName(); //вывод: "cwidget" QLayout *lt = qobject_cast<QLayout*>( button1->parent() ); if( !lt ) qDebug() << tr("Виджет не расположен в layout"); //собственно это и имеем else qDebug() << lt->objectName();
C++ (Qt)QWidget *parentWidget = button1->parentWidget(); // Пробуем получить вышестоящий виджет-владелец.QLayout *lt = ( parentWidget )? parentWidget->layout() : 0; // Если он есть, то возвращаем его слой.
C++ (Qt)static bool qu_find_widget_layout_( QLayout* li, QWidget* w, QLayout** lay ){ if( !li ) return false; if( li->widget() == w ) return true; if( QLayout* l = qobject_cast< QLayout* >( li ) ) { QLayoutIterator it = l->iterator(); QLayoutItem *li; while( ( li = it.current() ) != 0 ) { ++it; if( li->widget() == w || qu_find_widget_layout_( li->layout(), w, lay ) ) { if( *lay == 0 ) *lay = l; return true; } } } return false;} QLayoutItem* QUFindWidgetLayout( QWidget* w, int* row = 0, int* col = 0 ){ if( row ) *row = -1; if( col ) *col = -1; if( !w ) return 0; QWidget* pw = w->parentWidget(); QLayout* lay = pw->layout(); if( !lay ) { QList< QLayout* > l = pw->findChildren< QLayout* >(); if( !l.isEmpty() ) lay = l.at( 0 ); } retry: if( !lay || !qu_find_widget_layout_( lay, w, &lay ) ) return 0; if( QGridLayout* l = qobject_cast< QGridLayout* >( lay ) ) { int index = l->indexOf( w ); if( index >= 0 ) { int r, c, rs, cs; l->getItemPosition( index, &r, &c, &rs, &cs ); if( row ) *row = r; if( col ) *col = c; return l; } } else if( QBoxLayout* l = qobject_cast< QBoxLayout* >( lay ) ) { int index = l->findWidget( w ); if( index >= 0 ) { if( row ) *row = index; return l; } } QLayoutIterator it = lay->iterator(); QLayoutItem *l; while( ( l = it.current() ) != 0 ) { ++it; if( l->layout() ) { lay = l->layout(); goto retry; } } return 0;}
C++ (Qt)void getWidgetLayout(QObject* cur_obj, QString WidgetName, QString <_name){ QLayout *lt = qobject_cast<QLayout*>(cur_obj); if(lt) { QString layout_name = lt->objectName(); QString ob_name=""; if((!layout_name.isEmpty()) && (layout_name != "_layout")) for (int i=0, lcount=lt->count(); i < lcount; ++i) { ob_name=""; if(lt->itemAt(i)->widget()) ob_name = lt->itemAt(i)->widget()->objectName(); if(ob_name == WidgetName){ lt_name = layout_name; return; } } } QObjectList objch_list = cur_obj->children(); QObjectList::iterator objch_it = objch_list.begin(); QObject * chobj; while((objch_it != objch_list.end()) && (lt_name.isEmpty())) { chobj = *objch_it; getWidgetLayout(chobj, WidgetName, lt_name); ++objch_it; } }
C++ (Qt) if(cur_obj->isWidgetType()) { QWidget *topLvWidget = qobject_cast<QWidget*>(cur_obj)->topLevelWidget(); QString lt_name=""; getWidgetLayout(qobject_cast<QObject*>(topLvWidget), obj_name, layout_name); if(!lt_name.isEmpty())qDebug()<<lt_name; }