how we can define global QGraphicsScene *scene2; in our source?
-
hi,in my source i had QGraphicsScene *scene2;
my question is how i can convert it to global class that is a access able by other class?QGraphicsScene *scene2; QGraphicsView *view2;
thanks for reply
-
hi
There is no such thing as globally classes that are available to all classes.While it is possible to make them global to whole program its bad idea and leads to
buggy code fast - so I wont talk about globals anymore. :)However, the correct/better way is via the constructor.
example:
mainwindow class has
QGraphicsScene *scene2;
QGraphicsView *view2;and we have this other class TheAmazingViewer dialog that need to use these.
so in mainwindow we have the
void OpenViewer() {
TheAmazingViewer myAView ( scene2, view2); <---- we give via constructor
myAView.exev();
}the TheAmazingViewer constructor will then keep those 2 parameters for use in its functions.
Hope it makes sense
:) -
hi
There is no such thing as globally classes that are available to all classes.While it is possible to make them global to whole program its bad idea and leads to
buggy code fast - so I wont talk about globals anymore. :)However, the correct/better way is via the constructor.
example:
mainwindow class has
QGraphicsScene *scene2;
QGraphicsView *view2;and we have this other class TheAmazingViewer dialog that need to use these.
so in mainwindow we have the
void OpenViewer() {
TheAmazingViewer myAView ( scene2, view2); <---- we give via constructor
myAView.exev();
}the TheAmazingViewer constructor will then keep those 2 parameters for use in its functions.
Hope it makes sense
:)thanks for guiding me.your idea is not better even it's best:).
-
thanks for guiding me.your idea is not better even it's best:).
@stackprogramer
Hi
The concept of using constructors for such things is part of something they call RAII
http://www.tomdalling.com/blog/software-design/resource-acquisition-is-initialisation-raii-explained/:)