Home > front end >  Alternate scenes in a QGraphicsView
Alternate scenes in a QGraphicsView

Time:10-30

I have multiple QGraphicsScene objects that should be drawn into a single QGraphicsView at different times.

Once I've assigned a scene to a view via setScene member function it's possible to change the viewed scene with another?

Is it safe to assign nullptr via setScene if I don't want to show any scene or I have to pass a valid pointer each time?

Example

class MyViewer : public QObject{
Q_OBJECT
  QGraphicsView sc;
  QGraphicsScene scenes[10];
  int i;
public:
  //....
  QGraphicsView *getView() { return ≻}

  //called by a QTime event for example
  void update(){
    if(i >= 10)
      i = 0;
    sc.setScene(&(scene[i]));
    i  ;
  }
};

int main(int n, const char **a){
  QApplication app(n, a);
  MyViewer vw;
  /*
   * Do some stuffs, initialize and connect a QTimer
   */
  vw.getView()->show();
  app.exec();
}

CodePudding user response:

It's possible to change the viewed scene with another?

You can change the scene whenever you want, the documentation does not indicate a prohibition so you can do it.

Is it safe to assign nullptr via setScene if I don't want to show any scene or I have to pass a valid pointer each time?

Qt checks if the pointer is valid and accordingly performs the actions so it is safe to pass nullptr to remove the previous scene.


In Qt when something is dangerous and not allowed then it will be explicitly indicated in the documentation. It is also easy to see the source code to understand the behavior.

  • Related