简体   繁体   English

C ++ QT如何从QMainWindow布局访问QWidget的公共类成员

[英]C++ QT How to access a public class member of a QWidget from a QMainWindow Layout

as the title says, i want to access the member variables of a class that inheritates of QWidget from a QGridLayout in a QMainWindow. 如标题所示,我想访问从QMainWindow中的QGridLayout继承QWidget的类的成员变量。 I am able to access member functions of the QWidget class, but i cant reach the members of my "Player" class. 我可以访问QWidget类的成员函数,但无法访问“ Player”类的成员。

I am aware, that "->widget()" returns just a QWidget*. 我知道,“-> widget()”仅返回QWidget *。 Is there another way to return the real class, which sits at this coordinates? 还有另一种方法可以返回位于此坐标处的真实类吗?

This question shows just to access functions of the QWidget but not of classes inheritated of QWidget. 该问题仅显示访问QWidget的功能,而不显示访问QWidget继承的类的功能。

Code of the QMainWindow class: QMainWindow类的代码:

...
for(int row = 0; row < rowsCount; row++) {
    for(int col = 0; col < colsCount; col++) {
       QWidget *player = this->ui->gridLayout->itemAtPosition(row, col)->widget();
       player->[HERE I WANT TO ACCESS THE PUBLIC MEMBER]
    }
}
...

If I well understand you just have to dynamic cast your widget to a Player and check you really have a Player by security : 如果我很了解,您只需将小部件动态转换为Player并通过安全性检查您是否确实有Player:

QWidget *widget = this->ui->gridLayout->itemAtPosition(row, col)->widget();
Player * player = dynamic_cast<Player *>(widget);

if (player != NULL) {
   ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从父QWainWindow的QWidget访问QMainWindow - How to access QMainWindow from a QWidget which parent is not QMainWindow 将QWidget推广到QMainWindow或将QMainWindow从Qt Designer添加到QWidget - Promote QWidget to QMainWindow or add QMainWindow to QWidget from Qt Designer C ++:从类外部访问公共成员函数 - C++: Access to a public member function from outside of a class 如何通过封闭 class 访问嵌套的 class 成员 function,其中所有成员在 Z6CE809EACF90BA125B40FA4BD9 - How to access nested class member function from enclosing class where all the members are public in c++? 如何从自己类的成员函数中访问公共变量? (C ++) - How to access public variables from within their own class's member functions? (C++) Qt:从类 MainWindow 访问函数:其他文件中的公共 QMainWindow - Qt: Accessing function from class MainWindow : public QMainWindow in other file 如何从布局 QT 中正确删除 QWidget? - How to properly delete QWidget from layout QT? Qt for iOS:如何从Qt应用程序(iefrom QMainWindow或QWidget或其他任何东西)打开iOS组件? - Qt for iOS: How to open iOS component from Qt Application (i.e.from QMainWindow or QWidget or anything)? QLayout类和QMainWindow Qt C ++之间的继承 - Inheritance between class QLayout and class QMainWindow Qt c++ QT C++ 访问另一个类的类成员 - QT C++ access to a Class Member of another Class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM