[英]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.