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. Delete a clicked object from the scene?
QtWS25 Last Chance

Delete a clicked object from the scene?

Scheduled Pinned Locked Moved General and Desktop
16 Posts 3 Posters 3.6k Views
  • 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.
  • M Offline
    M Offline
    Marcius
    wrote on last edited by
    #1

    Well, title says it all. I'm only experimenting now before I can proceed with my program. I've got a function
    @
    void Card::mousePressEvent(QGraphicsSceneMouseEvent * event)
    @

    In the scene, I add items as Card class objects. How do I delete one if clicked?

    And maybe one more question under the same topic. This is my function to check if the move is legal:
    @
    bool IsLegal(string game, int player, Card& card, vector<Card>& cards)
    @

    Card& card - how do I pass the clicked object here?

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      Well, if you want delete on click you need reimplement mousePressEvent for GraphicsScene or emit some signal from item.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Marcius
        wrote on last edited by
        #3

        I get that much. Could I please get a little more specifics? I'm really struggling with signals / slots here. Getting output to qDebug is pretty much the extent of my knowledge at this stage on this..

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          qxoz
          wrote on last edited by
          #4

          Well, first simple solution which cones to my mind is:
          in your Card class
          @signals:
          void itemClicked();
          //....@
          then in press event
          @void Card::mousePressEvent(QGraphicsSceneMouseEvent * event)
          {
          emit itemClicked();
          }@
          for identifying clicked object you can use "QSignalMapper":http://qt-project.org/doc/qt-5.0/qtcore/qsignalmapper.html or "QObject::sender()":http://qt-project.org/doc/qt-4.8/qobject.html#sender

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Marcius
            wrote on last edited by
            #5

            [quote author="qxoz" date="1395929064"]
            for identifying clicked object you can use "QSignalMapper":http://qt-project.org/doc/qt-5.0/qtcore/qsignalmapper.html or "QObject::sender()":http://qt-project.org/doc/qt-4.8/qobject.html#sender [/quote]

            Ah, this is the missing puzzle piece. Exactly what I needed, thank you!

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Marcius
              wrote on last edited by
              #6

              I want to come back to this, because I can't seem to get things together.

              It seems to me that if I could get to return the object clicked in my scene (that being a card), I could finally move on. Documentation just confuse me further, even my own code has the same effect at the moment..

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

                Hi,

                Pseudo code:
                @
                MyClass::whereItNeedsToBeFunction(){
                // Create the card
                connect(card, SIGNAL(clicked()), this, SLOT(cardClicked());
                }

                void MyClass::cardClicked()
                {
                CardClass *card = qobject_cast<CardClass *>(sender());
                if (card) {
                // do what you want with the card
                }
                }
                @

                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
                • M Offline
                  M Offline
                  Marcius
                  wrote on last edited by
                  #8

                  Thank you again, SGaist! I'll work on this as soon as I get a chance. Before I start though, I'm using visual studio. Would "connect" be recognized?

                  EDIT

                  Or any other piece of this that's only a part of Qt environment?

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

                    Since you are already compiling a Qt project with VS, there should not be any problem

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

                      Hm, when I type 'connect' it's not in the pop-up list.

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

                        Your Card class is not a QObject ?

                        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
                        • M Offline
                          M Offline
                          Marcius
                          wrote on last edited by
                          #12

                          Only QGraphicsPixmapItem. I thought though that it derives from QObject

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

                            No they are not, for their original use case, deriving from QObject would not make sense. If you need signals and slots in a QGraphicsItem there's also QGraphicsObject to have a look at

                            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
                            • M Offline
                              M Offline
                              Marcius
                              wrote on last edited by
                              #14

                              Oh boy, wish I had known that before hand. Back to rewriting chunks of code

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

                                The documentation is your friend ;)

                                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
                                • M Offline
                                  M Offline
                                  Marcius
                                  wrote on last edited by
                                  #16

                                  Hint taken :) At that point I didn't know what sort of functionality I was going to need, plus I was only starting out with Qt. Oh well, lesson learned :)

                                  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