Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    The best way to extend QGraphicsItem

    General and Desktop
    2
    2
    1826
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J
      jh224 last edited by

      I want to extend QGraphicsItem to add "QString toXML()" and "void fromXML(QString xml)" to all of my custom GraphicsItem widgets. What is the best way to this? The reason for wanting to this, is to iterate through all of my GraphicsScene and save the graphicsScene and restore the graphicsScene. The way I have to do this now is to determine the type and then cast the item to the right widget object. However, it would be much nicer not to determine the type and to cast.

      I was thinking, maybe the best way is to make my own base Class that inherits QGraphicsItem. However my custom widgets don't directly inherit QGraphicsItem but rather QGraphicsEllipseItem, QGraphicsRectItem, QGraphicsPolygonItem...etc. Any suggestions?

      1 Reply Last reply Reply Quote 0
      • A
        Asperamanca last edited by

        I have found two possible ways:

        1. Create a wrapper base class derived from QGraphicsItem (or QGraphicsObject or QGraphicsWidget), which contains the items you currently use. Your current items become child items to this new (invisible) class.
          The class need not stay invisible, though. It could implement common drawing behavior and common user interaction behavior.

        2. Create an abstract base class, and let your items derive from both QGraphics* and that abstract class. Here, you have to take care that you do not run into any naming conflicts with the existing hierarchy of QGraphics* classes. If you want to share code between your classes, this doesn't really help you, but it does give you a clean interface to access all your classes with.
          But "Inheritance is not for code reuse anyway":http://www.parashift.com/c++-faq/smalltalk-and-inherit.html

        1 Reply Last reply Reply Quote 0
        • First post
          Last post