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. How can I detect collision among QPixmaps and QGraphicsItems objects

How can I detect collision among QPixmaps and QGraphicsItems objects

Scheduled Pinned Locked Moved General and Desktop
30 Posts 4 Posters 15.8k 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.
  • D Offline
    D Offline
    DSlim
    wrote on last edited by
    #3

    If I change to a QGraphicsPixmapItem can I make another list and collide the two. i.e. collideWithItem?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Asperamanca
      wrote on last edited by
      #4

      Yes, it should work that way.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DSlim
        wrote on last edited by
        #5

        I understand what you're saying about collision but the issues is I have the images stored in an array.

        @ main_targets[0] = QPixmap(":images/darkbluelogo.jpg");
        main_targets[1] = QPixmap(":images/graylogo.jpg");
        main_targets[2] = QPixmap(":images/lightbluelogo.jpg");
        main_targets[3] = QPixmap(":images/lime.jpg");
        main_targets[4] = QPixmap(":images/pink.jpg");
        main_targets[5] = QPixmap(":images/purple.jpg");
        main_targets[6] = QPixmap(":images/redlogo.jpg");
        main_targets[7] = QPixmap(":images/yellow.jpg");
        main_targets[8] = QPixmap(":images/brown.jpg");@

        I ran into alot of errors trying to store this array in a QGraphicsPixmapItem

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andreyc
          wrote on last edited by
          #6

          @
          QVector<QGraphicsPixmapItem*> targetItems;
          foreach(QPixmap pix, main_targets) {
          QGraphicsPixmapItem* item = QGraphicsPixmapItem(pix);
          targetItems.append(item);
          }
          @

          Hope it help.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DSlim
            wrote on last edited by
            #7

            Thanks it helps but implementation throws @In instantiation of 'class QForeachContainer<QPixmap [9]>':@
            The main_target array, QPixmap and targetitems array, QGraphicsPixmapItem seem to not play well together.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andreyc
              wrote on last edited by
              #8

              How did you define main_targets?
              Is it
              @
              QVector<QPixmap> main_targets;
              or
              QPixmap main_targets[9];
              @

              Qt specific keyword foreach works only with container classes like first definition. If you have second definition then you should use standard C++ for or while loop.

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

                Hi,

                foreach must be used with a containers. A c array is not considered a container.

                In your case you can use a classic for loop or change your array to a QList of QPixmap

                Hope it helps

                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
                • D Offline
                  D Offline
                  DSlim
                  wrote on last edited by
                  #10

                  Changing to containers I get the index is out of range error. Can I not assign each QPixmap an array value?

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andreyc
                    wrote on last edited by
                    #11

                    If you use containers then you should use append() method to add pixmaps to your container.

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      DSlim
                      wrote on last edited by
                      #12

                      I thought the @QVector<QGraphicsPixmapItem*> targetItems;
                      foreach(QPixmap pix, main_targets) {
                      QGraphicsPixmapItem* item = QGraphicsPixmapItem(pix);
                      targetItems.append(item);
                      }@ takes each instance of the main_targets array, and creates QGraphicsPixmapItems of them.

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andreyc
                        wrote on last edited by
                        #13

                        Could you show the code where you are getting "index is out of range" error.

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          DSlim
                          wrote on last edited by
                          #14

                          I pass the pmap to the MainTarget class by pointer, which was previously by reference. There isn't a specific line that the compiler is showing. It crashes on execution. I replaced main_targets[index] with targetItems since that should hold the QGraphicsPixmapItem items.

                          @void Dialog::setPixmaps()
                          {
                          //Targets are delivered every 1sec
                          Five_timer->start(1000);

                          QList<QGraphicsPixmapItem*> targetItems;
                          foreach(QPixmap pix, main_targets) {
                          QGraphicsPixmapItem* p_item = new QGraphicsPixmapItem(pix);
                          targetItems.append(p_item);
                          }
                          
                          int StartY = 0;
                          
                          //Random starting Y point between -140 and -389
                          StartY = (qrand() % (-400 + 150) -140);
                          
                          index = (qrand() % 9);
                          {
                              if(index % 2 == 0)
                              {
                                  //When even is selected start at far right
                                  pmap = new MainTargets(targetItems[index], 0);
                                  pmap->setPos(345,StartY);
                              }
                          
                              else
                              {
                                  //When odd is selected start at far left
                                  pmap = new MainTargets(targetItems[index], 1);
                                  pmap->setPos(-325,StartY);
                          
                              }
                          }
                          
                          scene->addItem(pmap);
                          

                          }@

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            andreyc
                            wrote on last edited by
                            #15

                            Does it always crash on the same place?
                            Try to step through the function using a debugger.

                            What is the type of main_targets in line #9?

                            1 Reply Last reply
                            0
                            • D Offline
                              D Offline
                              DSlim
                              wrote on last edited by
                              #16

                              QList<QPixmap> main_targets is the type.

                              Seems to fail at the QList::operator[].

                              That is why I think the main_target array seems to be the issue.

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                andreyc
                                wrote on last edited by
                                #17

                                How do you fill main_target?

                                1 Reply Last reply
                                0
                                • D Offline
                                  D Offline
                                  DSlim
                                  wrote on last edited by
                                  #18

                                  Guess that is where the problem is I thought the @ QList<QGraphicsPixmapItem*> targetItems;
                                  foreach(QPixmap pix, main_targets) {
                                  QGraphicsPixmapItem* p_item = new QGraphicsPixmapItem(pix);
                                  targetItems.append(p_item);@ fills the main_target list. If not what is the purpose of this line?

                                  1 Reply Last reply
                                  0
                                  • A Offline
                                    A Offline
                                    andreyc
                                    wrote on last edited by
                                    #19

                                    In this code you take the pixmaps from main_target and create qlist of qgraphicsitems.
                                    So, the main_target must be populated by the qpixmaps before this code.

                                    1 Reply Last reply
                                    0
                                    • D Offline
                                      D Offline
                                      DSlim
                                      wrote on last edited by
                                      #20

                                      @ main_targets[0] = QPixmap(":images/darkbluelogo.jpg");
                                      main_targets[1] = QPixmap(":images/graylogo.jpg");
                                      main_targets[2] = QPixmap(":images/lightbluelogo.jpg");
                                      main_targets[3] = QPixmap(":images/lime.jpg");
                                      main_targets[4] = QPixmap(":images/pink.jpg");
                                      main_targets[5] = QPixmap(":images/purple.jpg");
                                      main_targets[6] = QPixmap(":images/redlogo.jpg");
                                      main_targets[7] = QPixmap(":images/yellow.jpg");
                                      main_targets[8] = QPixmap(":images/brown.jpg");@

                                      The main_targets are created and stored in the constructor initially before anything is done. So the qlist should have 9 qpixmaps.

                                      1 Reply Last reply
                                      0
                                      • A Offline
                                        A Offline
                                        andreyc
                                        wrote on last edited by
                                        #21

                                        If I'm not mistaken you need to use .append() method to add elements to the QList. QList::opeartor[] does not work for adding new elements.

                                        EDIT: Yep, you have to use append() or operator+=() to extend a list "operator[]":http://qt-project.org/doc/qt-5/qlist.html#operator-5b-5d requires an index to be valid (0 <= i < qlist.size())

                                        1 Reply Last reply
                                        0
                                        • D Offline
                                          D Offline
                                          DSlim
                                          wrote on last edited by
                                          #22

                                          Thank you finally have it switched over to QGraphicsPixmapItems. Now the initial problem and see if I can get the collision working.

                                          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