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. Best way to handle ...
Forum Updated to NodeBB v4.3 + New Features

Best way to handle ...

Scheduled Pinned Locked Moved General and Desktop
qgraphicsitem
4 Posts 2 Posters 1.3k Views 2 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.
  • W Offline
    W Offline
    Walux
    wrote on last edited by
    #1

    Hi Qt Community,

    I'm just looking for the best way to handle this situation , which I'm sure that many Qt programmers have encountered (but have easily found a solution for it :b)

    In short terms , I have this class A :

    Class A : public QGraphicsItem
    {
    // Constructors , destructor etc...
    void doSmth ();
    // Some attributes...
    }
    

    And , I have a

    class B : public QGraphicsItem{//stuff}
    

    So in my subclassed QGraphicsScene , I want to catch the collidingItems of class B , but since it returns a list of QGraphicsItem :

    SubScene::method ()
    {
    //bItem is one of the attributes of my subScene
    QList<QGraphicsItem*> colItems = bItem->collidingItems ();
    for (int i=0;i <colItems.size ();i++)
    {
    If (dynamic_cast <A*>(colItems.at (i)))
          colItems.at (i)->doSmth (); //obviously impossible 
    }
    }
    

    I have tried to use ready Lists of class A , but i dont believe them to be the best and safest solution to this.

    Any ideas ? I'll be grateful ;)

    Taking things from beginning to end : That's my entertainment !

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

      Hi,

      It should rather be:

      A * aItem = dynamic_cast(colItems.at(i));
      if (aItem) {
          aItem->doSmth();
      }
      

      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
      5
      • W Offline
        W Offline
        Walux
        wrote on last edited by
        #3

        Awesome , I never knew it can work this way , however ... doesn't this trick use slightly more memory , or it only helps to build up the reference ?

        Taking things from beginning to end : That's my entertainment !

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

          It's not a trick, that's how you use dynamic_cast.

          You don't create a new object. You will use a local variable that is a pointer to your object or null if colItems.at(i) is not a pointer to an A object. Local variable that will be destroyed at the end of the function.

          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