I am trying to create a scene with Qt to display what I want (lets say for now just a text), but with the code describing the content being in another class.
I have a mainwindows, created with QtCreator gui, which contains the widget "graphicView"
In my mainwindows.cpp, I can do :
And it will display the text ('Hi!'). Now what I want to have is a class MyClass in charge of what is inside the widget graphicsView, and do in mainwindows.cpp:
- QMainWindow might contain several widget; you can set the main one by setCentralWidget().
- In the Qt libraries, ‘parent’ objects take care of destroying the contained objects (please, note this is not related to the C++ parent-child inheritance, but to ownership!).
So, to avoid memory leaks, you’d better always give ‘parents’ to your objects (methods like setCentralWidget() do that by default, anyway).
- When you use a QGraphicsScene, you usually need a QGraphicsView.
- If you want your QGraphicsScene to be owned by QMainWindow, but ruled by another class, perhaps the simplest method is:
- define the QGraphicsScene inside the QMainWindow
- take ownership by setCentralWidget()
- then pass a pointer to it to the other class.