PDA

View Full Version : pass Model between classes



Omid123
20th February 2015, 21:46
Hi there,

I am adding a new class, let say ClassA, to my QT project (in addition to mainwindow.cpp class). ClassA makes a Model, say modelA, of type QStandardItemModel. Then, I need to use modelA in different functions of my main QT class (i.e., all over mainwindow.cpp).

in MainWindow::MainWindow, I do:
ClassA objectA;
objectA.modelA......

This gives me access to modelA in the constructor, but not in other functions in mainwindow.cpp.

How can I make modelA recognized in all mainwindow.cpp functions?


modelA, in the header file of ClassA, is defined under 'public:' already.


Thanks,
Omid

d_stranz
20th February 2015, 22:45
This question has nothing to do with Qt. It's a basic C++ question. Do you actually know C++? You don't seem to understand that when you create an instance of something on the stack, it disappears as soon as the place where it is created goes out of scope. So if you declare a variable of type ClassA on the stack in your MainWindow constructor, it lives only as long as the constructor code does. As soon as the constructor is done, poof, your ClassA instance is gone.

Go back to your C++ book (if you have one) and see if you can teach yourself how to make instances of *anything* live beyond the scope of the method in which they are instantiated. (Hint: look up "pointer", "member variable", "stack", and "heap").

We aren't here to teach you C++. You're expected to know that before you start your Qt programming.