Results 1 to 5 of 5

Thread: How to send information between two classes where one is instantiated from the other?

  1. #1
    Join Date
    May 2010
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question How to send information between two classes where one is instantiated from the other?

    Qt Code:
    1. #include "apple.h"
    2. class Basket
    3. {
    4. ...
    5. int number_of_apples;
    6. Apple instantiatedApple;
    7. }
    To copy to clipboard, switch view to plain text mode 

    Sorry about asking this general object-oriented question, but I could not find relevant pages with google.

    The question is simple, but I cannot figure out the answer...

    How to send information between two classes where one class is instantiated inside the other?

    Example:

    Qt Code:
    1. class Apple
    2. {
    3. void Apple::methodinsideapple()
    4. {
    5. (how would you get current number_of_apples in basket???????)
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    I have lots of QDomDocuments combined, and want to build a chart of them. I use a QWizard to ask for details for a specific chart from the user. QWizard is in a separate class as it can only be created like that. How can I pass the dynamic info based on user interaction back and forth between mainwindow class and QWizard class.

    The only workaround that comes to my mind is to pass all the documents to the wizard class right at the beginning in its constructor, but then all the methods to filter info from the documents should be copied inside this class, too...

    Pls, help! Thx!

  2. #2
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Thumbs up Re: How to send information between two classes where one is instantiated from the ot

    How to send information between two classes where one class is instantiated inside the other?
    (how would you get current number_of_apples in basket???????)
    hi,
    just pass the object in parameter,

    Qt Code:
    1. class Apple
    2. {
    3. void Apple::methodinsideapple(Basket *b)
    4. {
    5. int total=b->number_of_apples;
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    and in Basket class method (cpp file),
    Qt Code:
    1. instantiatedApple.methodinsideapple(this)
    To copy to clipboard, switch view to plain text mode 

    hope it helps
    bala
    Last edited by BalaQT; 28th March 2011 at 08:07.

  3. The following user says thank you to BalaQT for this useful post:

    falconium (28th March 2011)

  4. #3
    Join Date
    Jul 2010
    Location
    Indonesia
    Posts
    83
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: How to send information between two classes where one is instantiated from the ot

    How to send information between two classes where one class is instantiated inside the other?
    You have to provide a method to access that class.
    Example:
    Qt Code:
    1. // basket.h
    2. #include "apple.h"
    3. class Basket
    4. {
    5. ...
    6. int number_of_apples;
    7. Apple instantiatedApple;
    8.  
    9. public: // accessor
    10. int numberOfApples() const
    11. { return number_of_apples; }
    12. }
    To copy to clipboard, switch view to plain text mode 
    And below is a simple mechanism on how to access it.
    Qt Code:
    1. // apple.h
    2. class Basket;
    3.  
    4. class Apple
    5. {
    6. Basket* basket;
    7. void methodinsideapple();
    8.  
    9. public:
    10. inline void setBasket(Basket* basket)
    11. { this->basket = basket; }
    12. }
    13.  
    14. // apple.cpp
    15. #include "apple.h"
    16. #include "basket.h"
    17.  
    18. Apple::methodinsideapple()
    19. {
    20. int numOfApples = basket->numberOfApples(); // you got it
    21. ...
    22. }
    To copy to clipboard, switch view to plain text mode 

    How can I pass the dynamic info based on user interaction back and forth between mainwindow class and QWizard class.
    You can use similar technique to solve your real problem. Maybe design pattern will be a great solution for complex class interaction.

  5. The following user says thank you to viulskiez for this useful post:

    falconium (28th March 2011)

  6. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to send information between two classes where one is instantiated from the ot

    I would recommend viulskiez's approach over balas. The former provides useful encapsulation over the data which the later does not.

  7. The following user says thank you to squidge for this useful post:

    falconium (28th March 2011)

  8. #5
    Join Date
    May 2010
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to send information between two classes where one is instantiated from the ot

    Ahh, I see... so basically, the easiest would be to pass the pointer variable to the constructor of the instantiated class.
    Yesterday I was trying to find a solution, and came to the idea of slots/signals, but that is much more complex to maintain properly.
    Thank you!
    Last edited by falconium; 28th March 2011 at 17:36.

Similar Threads

  1. construction of classes within classes
    By mobucl in forum Newbie
    Replies: 8
    Last Post: 10th January 2011, 14:51
  2. Access instantiated objects from C++
    By Sunny31 in forum Qt Quick
    Replies: 3
    Last Post: 5th December 2010, 18:25
  3. ActiveQt : requested control could not be instantiated
    By zirconius in forum Qt Programming
    Replies: 2
    Last Post: 9th April 2010, 14:27
  4. Getting MAC Information from qt?
    By vishal.chauhan in forum Qt Programming
    Replies: 4
    Last Post: 23rd May 2007, 10:31

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.