Results 1 to 6 of 6

Thread: Advice needed - creating items

  1. #1
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Advice needed - creating items

    I need to create several items and add them to a scene. But for each item I need to set some properties.
    Ex.: item.color , item.speed , item.power , etc...

    Should I make a class, say "Item", give it those properties and then create a QGraphicsItem and add to the scene, or should I subclass a QGraphicsItem, add those properties and add it to the scene?

    Is there any difference in these two approaches? Is one "better" than the other?

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Advice needed - creating items

    I'm not sure what you mean or what the difference is you want to describe.

    However, some solutions are personal taste. If it works for you and you feel happy with a solution, and it doesn't bother you, nobody will tell you it is wrong.

    On the other hand, always keep your classes and their implementations as clean and simple as possible. If you can skip an extra object, do so. And keep in mind that if you want to make your program open source so other people can work on it too, you want to make the source as easy to read as possible and close to common standards.

  3. #3
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Advice needed - creating items

    I mean:
    This:
    Qt Code:
    1. class Item{
    2. QGraphicsItem * mainItem;
    3. int eficiency;
    4. int power;
    5. QString abilities;
    6. }
    To copy to clipboard, switch view to plain text mode 

    Versus this:
    Qt Code:
    1. class Item : public QGraphicsItem{
    2. int eficiency;
    3. int power;
    4. QString abilities;
    5. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Advice needed - creating items

    This is "inheritance vs composition". Depending on whom you ask and what your specific case is, it could go either way (rather both ways) as far as what is 'right'. I encourage you to pick up a book on the subject and/or google "inheritance vs composition" and decide for yourself.

  5. #5
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Advice needed - creating items

    I'll just go with inheritance.

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

    Default Re: Advice needed - creating items

    In the first case since the item doesn't inherit QGraphicsItem, you need some way to make the classes cooperate. For instance you need a way to map a QGraphicsItem to its Item counterpart as Graphics View operates in terms of QGraphicsItems.
    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. Advice needed for QT application architecture
    By hubbobubbo in forum Qt Programming
    Replies: 0
    Last Post: 14th December 2009, 12:30
  2. Thread design advice needed
    By jonks in forum Qt Programming
    Replies: 3
    Last Post: 22nd October 2009, 22:32
  3. Qtopia H/w Advice needed
    By void* in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 19th January 2007, 12:25
  4. Multiple Windows -> Advice needed
    By vokal in forum Newbie
    Replies: 2
    Last Post: 8th January 2007, 09:40
  5. Practical QTreeView: Advice needed
    By Jojo in forum Qt Programming
    Replies: 13
    Last Post: 20th February 2006, 21:28

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.