简体   繁体   中英

Qt: how to call main window's pointer from another class?

I want to separate my roles in a game to different .cpp and .h files, but I have trouble calling the pointer in mainwindow from another class, which I added through scene->add() in mainwindow.cpp.

It shows "player" was not declared in this scope when I run.

I know this question seems pretty basic, but I've tried my best to fix it with no results.

The following are main methods I've tried:

  • add MainWindow:: before player, but it shows the similar error that mainwindow is not declared in this scope .
  • revise player to this pointer, and it says that class Player has no such member.
  • revise player to pos() pointer, it says pos() is not declared in this scope
  • set class Player as friend with main window, but nothing changes

Here is the code:

main window.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    scene(new QGraphicsScene(0,0,800,600)),
    timer(new QTimer)
{
    ui->setupUi(this);
    ui->graphicsView->setScene(scene);

    player = new QGraphicsPixmapItem(QPixmap(":/img/whitedog.png").scaled(150,150));
    scene->addItem(player);
    player->setPos(0, 0);
    timer->start(10);
}

MainWindow::~MainWindow()
{
    delete ui;
}

main window.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QTimer>
#include <QKeyEvent>

#include "player.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);

    friend class Player;

    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QGraphicsScene *scene;
    QTimer *timer;
    QGraphicsItem *player;
};

#endif // MAINWINDOW_H

player.cpp

#include "player.h"

Player::Player()
{
}

void Player::keyPressEvent(QKeyEvent *e)
{

       switch(e->key()) {

       case Qt::Key_Up:
               player->setPos(player->x(), player->y()-10);
           break;
                 }
}

player.h

#ifndef PLAYER_H
#define PLAYER_H

#include <QKeyEvent>
#include <QGraphicsScene>
#include <QMainWindow>
#include <QGraphicsPixmapItem>
#include <QTimer>

#include "ui_mainwindow.h"


class Player
{

public:
    Player();
    void keyPressEvent(QKeyEvent *e);
};

#endif // PLAYER_H

You are getting confused between two different things.

You have a class Player , which I believe is the one that you want to declare in mainwindow.h but an object of class Player is not created anywhere( as far as what is shown above ).

The player currently declared in the mainwindow.h is of type QGraphicsItem and not of the Player class that you created, so you cannot expect your keyPressEvent to work on it.

You need to have a second look at your design, know the purpose of the Player class, it's responsibilities. Currently it is a simple class not inheriting from anything, did you want it to maybe inherit from QGraphicsItem .

In such a case, your class maybe could look something like this :

class Player : public QGraphicsItem
{
   //Players's responsibilities.
}

And in your mainwindow.h , you can just use the instance of the Player instead of using a QGraphicsItem instance.

class MainWindow : public QMainWindow
{
  ...// other stuff
  private:
  Player *player;
}

Additional Pointers :

1 . I don't see a reason for the Player class to be a friend class in mainwindow.h , but then again it is your design.

2 . Prefer initializing the player object part in the member initialization list rather than doing it in the constructor.

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