Singleton Item

  • Is it possible to have just an instance of visual item (singleton Rectangle) for the whole app? For example my app has 4 rectangles, each of these rectangle contains the same singleton Rectangle.

  • Don't think that's possible. If they were all the same instance, they would have to share the same property values, e.g. for x and y, and that would be kind of useless, wouldn't it? I'm sure there are plenty of other reasons too, this was just the first one I thought of.

    Why do you want to do this? I'm sure there are other ways to accomplish what you are trying to do.

  • This singleton object is (a) expensive to create and (b) compose of many components some of which are created on demand (that's why I intend not to have more than one). x and y could be modified to specify the position, but the width and height are fixed.

  • Moderators

    The nature of a singleton is that there is only one of them in existence. If you have multiples of it on screen, then -- by definition -- it can't be a singleton.

    You can't have "visual clones" of an object itself. The closest I can think of is if you do some magic by rendering an image of the master object to a pixmap and just use copies of that pixmap in the other locations.

  • You're right. It is not a singleton object... rather it is a flyweight object which is backed by a singleton. Is it possible to implement it in QML?

  • What you can do is separate all the visual aspects of the item from the data. Then you can have several instances of the visual item all sharing the same data. If it's the data that is expensive to to create, this should be a suitable solution. If it's the visual item that is expensive to create, then I'm really wondering what that visual item does and how it is created...

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.