0

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

1 Answers1

3

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);
}
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • Thanks!! What is the function of the term auto ? – Davide Ferrari Jun 01 '16 at 08:02
  • @DavideFerrari It stands in for a [compiler inferred type](http://stackoverflow.com/q/8935590/1329652). When used [judiciously](http://stackoverflow.com/q/6434971/1329652), it improves the readability of the code and lets you type less. Modern IDEs will infer the type just like a compiler would, so`mazzo` will get completion etc. – Kuba hasn't forgotten Monica Jun 01 '16 at 12:41