Davide Ferrari
Davide Ferrari

Reputation: 75

Error with QGraphicsScene *scene in constructor

hi i'm still having a problem with QGraphicsScene

I've created a widget called Gioco and i've declared the *scene in the constructor

Gioco::Gioco()
{
    QGraphicsScene *scene = new QGraphicsScene();
    scene -> setSceneRect(0,0,1980,1200);
    setScene(scene);
}

now I want to use the same *scene in a void but i get the error undefinied reference to *scene

void Gioco::partita()
{extern QGraphicsScene *scene;

    //create a new Pixmap Item
    QGraphicsPixmapItem *img_mazzo = new QGraphicsPixmapItem();
    img_mazzo -> setPixmap(QPixmap(":/Media/Immagini/dorso.jpg"));

    //add to scene
    scene -> addItem(img_mazzo);
}

how can I solve this error ? thanks

Upvotes: 0

Views: 267

Answers (1)

You get the error because the extern QGraphicsScene * scene declares a global variable that isn't defined anywhere.

You probably want the scene to be a member variable, and there's no need to use explicit dynamic allocation:

class Gioco {
  QGraphicsScene m_scene;
public:
  Gioco();
  void partita();
};

auto const kImmaginiDorso = QStringLiteral(":/Media/Immagini/dorso.jpg");

Gioco::Gioco() {
  m_scene.setSceneRect(0,0,1980,1200);
  setScene(&m_scene);
}

void Gioco::partita() {
    auto mazzo = new QGraphicsPixmapItem;
    mazzo->setPixmap(QPixmap(kImmaginiDorso));
    m_scene.addItem(mazzo);
}

Upvotes: 3

Related Questions