Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QGraphicsScene and Persistent References
Qt 6.11 is out! See what's new in the release blog

[SOLVED] QGraphicsScene and Persistent References

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 776 Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I apologize if this has been asked already -- if it has, I couldn't find it. At any rate -- is it ok/possible to store a persistent references (pointers) to objects inside a QGraphicsScene? For example, if I create a QGraphicsSimpleTextItem, and add it to a QGraphicsScene myScene, like so:

    @
    QGraphicsSimpleTextItem *myItem = new QGraphicsSimpleTextItem(QString("@"));
    myScene.addItem(myItem);
    @

    If, later on in this code, more objects are added/removed from myScene, myItem or other objects are moved, modified, etc., is there ever a scenario where the myItem pointer could become invalid (assuming the pointer stays in scope)? If so, is there a good/accepted way to store a persistent reference to objects in a QGraphicsScene?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Adding new objects won't change the memory address of older objects nor moving them on the scene etc.

      Depending on what you need these pointers for you can use one of Qt's containers like QVector/QList/QMap/QHash/QSet or if you only need to keep one item, a simple member variable would be enough.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved