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. Beginners: how to loop qphraphicsobjects in a scene
Qt 6.11 is out! See what's new in the release blog

Beginners: how to loop qphraphicsobjects in a scene

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.9k 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.
  • S Offline
    S Offline
    SherifOmran
    wrote on last edited by
    #1

    Hi Guys,

    I have custom objects in a game, with inheritance from QGraphicsObject = QGraphicsItems + QObject

    Now in the game, I want to make a foreach . can some body help me?

    I defined the objects this and then I added them to the scene

    @
    QPointer <Spirit> spitem = new Spirit(this);
    scene->addItem(spitem);
    @

    I tried this
    @
    foreach (Spirit c, spitem)
    {

    }

    and

    foreach (QPointer <Spirit> c, spitem)
    

    {
    }

    @

    but i get @QPointer is not a class@

    1 Reply Last reply
    0
    • C Offline
      C Offline
      codenode
      wrote on last edited by
      #2

      You need to include the correct header for QPointer.

      QGraphicsScene::items() then will give you the QList<QGraphicsItem*>, which you should use in foreach.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SherifOmran
        wrote on last edited by
        #3

        Hello my friend

        I understood it this way

        @
        QList<QGraphicsItem *> spirits = scene->items();
        foreach (QGraphicsItem *item, spirits) {
        }
        @

        But how can select certain spirits using their class names?

        1 Reply Last reply
        0
        • U Offline
          U Offline
          utcenter
          wrote on last edited by
          #4

          You can type check during runtime with qobject_cast or dynamic_cast and proceed accordingly. If the existing object matches the cast parameter you will get a valid pointer, if not the pointer will be null.

          If you have multiple QGraphicsItem derived classes and you need to proceed according to type for every one of them using a cascade of casts is probably a bad idea, in this case you need to create your own QGraphicsItem derived hierarchy with a virtual method to return like an enum of all the types, statically cast the scene->items() pointer to your base type and use a switch statement to switch to the correct case instead of going through them all with the dynamic cast.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SherifOmran
            wrote on last edited by
            #5

            Thank you guys.

            I made it using @item->setdata@ and @item->data@ While creating the item i set its data and in the game, I read its data.

            1 Reply Last reply
            0
            • U Offline
              U Offline
              utcenter
              wrote on last edited by
              #6

              Yes that is possible too, only it is on instance level instead of class level. It saves on creating your own hierarchy thou. I am not sure if it will end up using more memory than just having a method that returns a hard-coded enum value.

              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