Results 1 to 2 of 2

Thread: pass Model between classes

  1. #1
    Join Date
    Jan 2015
    Posts
    8
    Thanks
    7
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default pass Model between classes

    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

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: pass Model between classes

    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.

Similar Threads

  1. Replies: 1
    Last Post: 29th August 2013, 05:41
  2. Replies: 1
    Last Post: 7th August 2012, 12:41
  3. How to access Classes from another Classes
    By revellix in forum General Programming
    Replies: 1
    Last Post: 4th August 2011, 15:39
  4. Replies: 12
    Last Post: 7th January 2011, 13:21
  5. Default Item View classes and returning data from model
    By xerionn in forum Qt Programming
    Replies: 8
    Last Post: 23rd April 2009, 19:50

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.