2011-07-01 8 views
9

私は親からchildrenWidget(または子の子の子)からアクセス可能でなければならないソフトウェアを作成しています。子からparentWidget(または親の親の親...) 。例えばQTでトップ(ベース)のparentWidgetを取得するには?

QWidget_Principal --> WidgetApplications --> WidgetMenuBar --> PushButtonFullScreen. 

問題は、私はこれを行うことができる方法は、

this->parentWidget()->parentWidget()->parentWidget()->showFullScreen(); 

それを行うには、このそこに簡単な方法をやっているのですか?

ありがとうございました そして私の非常に悪い英語を申し訳ありません。

ルイス・ダ・コスタ

答えて

26

ウィジェットのウィンドウウィジェットを取得するためにQWidget * QWidget::window() constを使用しています。

QWidgetList QApplication::topLevelWidgets() [static]機能が

+0

をhttp://doc.qt.io/qt-4.8/qwidget.html#window – Vortexfive

2

ちょうどグローバル関数記述...あなたのアプリ内のすべてのトップレベルのウィジェットのリストを取得するには、もあります:

QWidget* TopLevelParentWidget (QWidget* widget) 
    { 
    while (widget -> parentWidget() != Q_NULLPTR) widget = widget -> parentWidget() ; 
    return widget ; 
    } 
+0

動的キャストを使用して、適切なQWidgetを取得するようにしてください。 – Jens

4

別のアプローチ:

QWidget* topWidget = QApplication::topLevelAt(yourWidget->mapToGlobal(QPoint())); 
関連する問題