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, if you want delete on click you need reimplement mousePressEvent for GraphicsScene or emit some signal from item.
-
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..
-
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
-
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?
-
Since you are already compiling a Qt project with VS, there should not be any problem
-
Hm, when I type 'connect' it's not in the pop-up list.
-
Your Card class is not a QObject ?
-
Only QGraphicsPixmapItem. I thought though that it derives from 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
-
Oh boy, wish I had known that before hand. Back to rewriting chunks of code
-
The documentation is your friend ;)
-
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 :)