Delete a clicked object from the scene?
-
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?
-
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 -
[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!
-
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..
-
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
-
Since you are already compiling a Qt project with VS, there should not be any problem
-
Your Card class is not a QObject ?
-
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
-
The documentation is your friend ;)