Results 1 to 9 of 9

Thread: getting QGraphichItem objct from custom class

  1. #1
    Join Date
    Nov 2010
    Location
    Budapest, Hungary
    Posts
    125
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default getting QGraphichItem objct from custom class

    Hi,

    I have a class which contains two objects whose classes are both derived from QGraphicsItem. I would like to reach them outside without setting these members public. In codes speaking it is something like this:
    Qt Code:
    1. class myClass
    2. {
    3. public: // ... and here I need some functions like getVar1() or getVat2()
    4. private:
    5. // ...
    6. c1 * var1; // c1 : public QGraphicsItem
    7. c2 * var2; // c2 : public QGraphicsItem
    8. }
    To copy to clipboard, switch view to plain text mode 

    It could be also fine to get them in a QGraphichScene object, but a function like below does not work because the copy constructor is private.
    Qt Code:
    1. QGraphicsScene myClass::getMyScene()
    2. {
    3. temp.addItem(var1);
    4. temp.addItem(var2);
    5. return temp;
    6. }
    To copy to clipboard, switch view to plain text mode 

    I've also tried this:
    Qt Code:
    1. void setMyScene(QGraphicsScene & sc)
    2. {
    3. sc.addItem(var1);
    4. }
    To copy to clipboard, switch view to plain text mode 
    but this does not seem to do anything to a "QGraphicsScene scene" variable. Frankly speaking it work like this:
    Qt Code:
    1. di->setMyScene(scene[0]);
    2. ui->graphicsView->setScene(scene);
    To copy to clipboard, switch view to plain text mode 
    But this solution does not really look correct to me. Well, works, but still...
    What would you suggest?
    Szilvi

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: getting QGraphichItem objct from custom class

    I would like to reach them outside without setting these members public.
    If there is only one place, or one class that needs to access these privates, then you can use the friend keyword for that class.
    If you need to access it from more than that, you probably have a design error, and should not use private there, or in turn, rethink why you need to access these privates from outside.
    EDIT: for some reason I thought your getter and setter method should be private.
    After reading again I see that only your member are private, so you can disregard the above statement.
    Qt Code:
    1. QGraphicsScene myClass::getMyScene()
    2. {
    3. temp.addItem(var1);
    4. temp.addItem(var2);
    5. return temp;
    6. }
    To copy to clipboard, switch view to plain text mode 
    I could not make any sense of the above code.
    You are talking about retrieving QGraphicsObject 's but return a temp scene??

    Even more anigmatic is your second code segment with setMyScene().

    Qt Code:
    1. di->setMyScene(*scene /*scene[0]*/); //what is 'di'?
    2. ui->graphicsView->setScene(scene); //that line of code is the only one making any sense in your code segments so far
    To copy to clipboard, switch view to plain text mode 

    Maybe if you explain what it is you are trying to achieve we can help you more.
    Last edited by high_flyer; 24th May 2011 at 08:51.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Nov 2010
    Location
    Budapest, Hungary
    Posts
    125
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: getting QGraphichItem objct from custom class

    yes, you are right, I didn't explain enough. "di" is a myClass object. "Outside" means basically "anything out of the class", in my application this would be like in codes above, so something else should get the private variables.
    So the code is like this:

    Qt Code:
    1. class c1 : public QGraphicsItem
    2. {
    3. //...
    4. }
    5. class c2 : public QGraphicsItem
    6. {
    7. //...
    8. }
    9.  
    10. class myClass
    11. {
    12. private:
    13. c1* var1;
    14. c2* var2;
    15. public:
    16. myClass();
    17. ~myClass();
    18. // ... (other public functions)
    19. void setMyScene(QGraphicsScene & qgs); // this one works as shown above
    20. };
    To copy to clipboard, switch view to plain text mode 

    and where I have a myClass object:

    Qt Code:
    1. myClass * mc = new myClass;
    2. // ... setting var1 and var2
    3. mc->setMyScene(*scene); // works
    4. ui->graphicsView->setScene(scene);
    To copy to clipboard, switch view to plain text mode 

    What do I minunderstand in the preveously shown getMyScene?
    Anyway there should be a better way.
    Szilvi

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: getting QGraphichItem objct from custom class

    "Outside" means basically "anything out of the class",
    Then create a public setter and getter for your private members, which I guess is what you have tried to do in the code snippets you posted.

    But as I said, you code and what you write in words does not match, so I have hard time following what it is you want to do.
    Assuming that what you want is:
    I have a class which contains two objects whose classes are both derived from QGraphicsItem. I would like to reach them outside without setting these members public.
    Then something like the following would do the job:
    Qt Code:
    1. class myClass
    2. {
    3. public: // ... and here I need some functions like getVar1() or getVat2()
    4. const c1 & getC1()const
    5. {
    6. return var1;
    7. }
    8.  
    9. const c2 & getC1()const
    10. {
    11. return var2;
    12. }
    13.  
    14. void setC1(const c1 &otherC1)
    15. {
    16. var1 = otherC1;
    17. }
    18.  
    19. void setC2(const c2 &otherC2)
    20. {
    21. var2 = otherC2;
    22. }
    23. private:
    24. // ...
    25. c1 * var1; // c1 : public QGraphicsItem
    26. c2 * var2; // c2 : public QGraphicsItem
    27. }
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Nov 2010
    Location
    Budapest, Hungary
    Posts
    125
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: getting QGraphichItem objct from custom class

    I highly appreciate your effort trying to help me.
    Your solution almost works, but gives this error message:
    " invalid initalization of redference of type 'cont c1&' from expression of type 'c1* const' "

    These are ok:
    const c1 * getC1() const { return val1;}
    const c1 & getC1() const { return *val1;}

    Which is better?
    Szilvi

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: getting QGraphichItem objct from custom class

    Your solution almost works, but gives this error message:
    " invalid initalization of redference of type 'cont c1&' from expression of type 'c1* const' "
    Sorry, I missed that your variables are pointers.

    These are ok:
    const c1 * getC1() const { return val1;}
    const c1 & getC1() const { return *val1;}
    In this case the first option is better, since you can return a NULL pointer if it is not initialized.
    The second option prerequisites that the pointer is initialized, which is dangerous.

    P.S
    This is basic C++.
    Be sure to learn C++ first, before working with Qt, you will constantly stumble on such problems.
    Last edited by high_flyer; 24th May 2011 at 11:03.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    szisziszilvi (24th May 2011)

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: getting QGraphichItem objct from custom class

    Please post threads that are related to C++ syntax and design in "General Programming" and not in "Qt Programming" forum, even though you are using some Qt class in your code.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #8
    Join Date
    Nov 2010
    Location
    Budapest, Hungary
    Posts
    125
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: getting QGraphichItem objct from custom class

    yes, you are perfectly right about this, but my problem raised whilst using qt-related classes and I tried the pasics in C++ and the error messages suggested me that the point would be around some feature of the Qt-class QGraphicsView from which I derived some other classes.
    Szilvi

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: getting QGraphichItem objct from custom class

    If your issue remains valid when replacing "QGraphicsScene" with "Foo" and "QGraphicsItem" with "Bar" then it is not a Qt-related issue and belongs to the General Programming forum.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Using custom class as a key (QSet)
    By daujeroti in forum Qt Programming
    Replies: 4
    Last Post: 28th April 2011, 20:39
  2. QSet and custom class
    By been_1990 in forum Qt Programming
    Replies: 12
    Last Post: 22nd January 2010, 16:36
  3. qhash with custom class
    By dognzhe in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2009, 06:30
  4. custom class for tslib
    By nrabara in forum Newbie
    Replies: 1
    Last Post: 28th April 2009, 13:15
  5. Custom Model Class
    By mattjgalloway in forum Qt Programming
    Replies: 12
    Last Post: 4th June 2007, 17:30

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.