Results 1 to 4 of 4

Thread: GraphicsLayer polymorphism concernings

  1. #1
    Join Date
    Aug 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default GraphicsLayer polymorphism concernings

    Hey, i`ve got following problem.
    I`m developing a GIS-like application an for that, i define a class SpatialItem from which all spatial items (like Polygon, Point, Line...) inherit. SpatialItem itself inherits from QGraphicsItem and QObject. All inherited spatial items (SpatialPolygonItem for instance) have their own implementation of the paint() method (inherited from QGraphicsItem). There is no problem so far.
    The thing is, every spatial item has a SpatialItemRenderer member, while SpatialPolygonItem of course has a SpatialPolygonRenderer member (which inherits from SpatialItemRenderer) and so on... A group of items with same type are now stored in a class SpatialLayer which is not inherited by any other class, respectively is not polymorphistic (because of many reasons). The SpatialItemRenderer is beeing given to the SpatialItems`s through the SpatialLayer (in form of a pointer), because all Items in a layer should have the same renderer. Now, in case that a layer can store various item types (as i sad: Point, Polygon...), and every item type has its own renderer type, how can i realize that in a method in the SpatialLayer class. The layer has a kind of polymorphistic character, because it can store different kinds of spatial items, but i guess i have to write a function like

    Qt Code:
    1. void SpatialLayer::setSpatialRenderer(SpatialItemRenderer *renderer)
    2. {
    3. switch(layerType) // layerType stores the type of the layer (Point, Polygon...)
    4. {
    5. case Point:
    6. SpatialPointRenderer *pointRenderer = dynamic_cast<SpatialPointRenderer *> renderer;
    7. break;
    8. case Polygon:
    9. SpatialPolygonRenderer *polygonRenderer = dynamic_cast<SpatialPolygonRenderer *> renderer;
    10. break;
    11. ...
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    Is that my only chance to realize it? My problem is, im not happy about casting, i would rather preferr a way where i can avoid it.
    Im in a pretty early development stage, im very happy about now. So i can change a lot of the concept i explained here.

    I hope at least anybody get what i wanted to say.

  2. #2
    Join Date
    Jan 2006
    Location
    Alingsås, Sweden
    Posts
    437
    Thanks
    3
    Thanked 39 Times in 39 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: GraphicsLayer polymorphism concernings

    Well, looking at this from a general perspective, what actually happens when you rely on polymorphism is that the compiler designs the switch case for you (or rather, uses the vtable to implement it). Now, what you can do to avoid the switch case (which would allow you to expand you set of classes in a more flexible manner) is to add yet another layer of classes, with the sole purpose of introducing polymorphism around this method. Not sure if that simplifies matters though...

  3. #3
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: GraphicsLayer polymorphism concernings

    I wouldnt do it like that.
    I would do a QList of SpatialLayer object. SpatialLayer would have only attributes like layer name, color, on, off, blablabla....
    Then you shoud have a pointer to SpatialLayer object, in your SpatialItemRenderer .
    Inside each SpatialItemRenderer method, check the atributes of SpatialLayer, to choose the color, wheter it's on or off, ...

    The thing is, every spatial item has a SpatialItemRenderer member, while SpatialPolygonItem of course has a SpatialPolygonRenderer
    Don't understand why you're doing this, I think would be cleaner redering from the inerithed paint() of lines, points,... Besides if you create different methods names like SpatialPolygonRenderer how do plan taking advantage of polymorphism ?
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  4. #4
    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: GraphicsLayer polymorphism concernings

    SpatialItemRenderer should have an interface common to all its subclasses and you should never need to cast the renderer to a specific subclass.
    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. Polymorphism interfaces in C++
    By ComaWhite in forum General Programming
    Replies: 2
    Last Post: 1st July 2009, 12:38
  2. polymorphism in java
    By mickey in forum General Programming
    Replies: 0
    Last Post: 13th November 2008, 16:34
  3. Polymorphism with static calls
    By niko in forum General Programming
    Replies: 12
    Last Post: 9th November 2007, 09:52

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.