简体   繁体   中英

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. I am able to access member functions of the QWidget class, but i cant reach the members of my "Player" class.

I am aware, that "->widget()" returns just a 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.

Code of the QMainWindow class:

...
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 :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM