Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tutorial6: when and why do I have to add *parent as a class parameter and when not? #5

Open
dsyleixa opened this issue Jul 9, 2023 · 0 comments

Comments

@dsyleixa
Copy link

dsyleixa commented Jul 9, 2023

hi,
as tp tutorial6:
when do I have to add *parent as a class parameter and when not?

e.g., not:
.cpp


Bullet::Bullet(): QObject(), QGraphicsRectItem(){
    setRect(0,0,10,50);
    QTimer * timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(move()));
    timer->start(50);
}

header:

class Bullet: public QObject, public QGraphicsRectItem{
    Q_OBJECT
public:
    Bullet(QGraphicsItem * parent=0);  // parent only in header
public slots:
    void move();
};

but e.g., yes:
.cpp


Enemy::Enemy(QGraphicsItem *parent): QObject(), QGraphicsRectItem(parent){ // <<<<<<<<<<<<<<<<<<<<<<<<<<<
    int random_number = rand() % 700;
    setPos(random_number,0);
    setRect(0,0,100,100);
    QTimer * timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(move()));
    timer->start(50);
}

header:

class Enemy: public QObject, public QGraphicsRectItem{
    Q_OBJECT
public:
    Enemy(QGraphicsItem * parent=0);  // parent in header like above
public slots:
    void move();
};

i.e., why can't I just declare
Enemy::Enemy(): QObject(), QGraphicsRectItem(){ ...}[/code]`
similar to the Bullet file?

(ref.: https://github.com/MeLikeyCode/QtGameTutorial/tree/master/tutorial6)

(edit,
BTW, in previous tutorials https://github.com/MeLikeyCode/QtGameTutorial/tree/master/tutorial1 ... https://github.com/MeLikeyCode/QtGameTutorial/tree/master/tutorial5 this "parents" thing has not been added at all for either class)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant