【问题标题】:How to get the Top(base) parentWidget in QT?如何在 QT 中获取 Top(base) parentWidget?
【发布时间】:2018-07-13 07:33:48
【问题描述】:

我正在创建一个软件,该软件必须由父级访问 childrenWidget(或子级的子级......)以及从子级访问 parentWidget(或父级的父级...... )。

例如:

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

问题是我能做到这一点的方式就是这样做

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

这有一个简单的方法吗?

提前致谢 对不起,我的英语很糟糕。

路易斯·达科斯塔

【问题讨论】:

    标签: qt widget parent


    【解决方案1】:

    使用QWidget * QWidget::window () const 为您的小部件获取窗口小部件。

    还有QWidgetList QApplication::topLevelWidgets () [static] 函数,用于获取应用中所有顶级小部件的列表...

    【讨论】:

      【解决方案2】:

      另一种方法:

      QWidget* topWidget = QApplication::topLevelAt(yourWidget->mapToGlobal(QPoint()));
      

      【讨论】:

        【解决方案3】:

        只写一个全局函数:

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

        【讨论】:

        • 最好使用动态转换来确保你得到一个合适的 QWidget。
        【解决方案4】:

        非常简单的 python 答案,当然可以在任何子小部件类中调用它:

        top_widget = self
        while top_widget.parentWidget(): top_widget = top_widget.parentWidget()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-02-13
          • 2021-11-03
          • 2021-02-02
          • 2011-01-28
          • 1970-01-01
          • 1970-01-01
          • 2020-12-19
          相关资源
          最近更新 更多