[solved] deal cards and add to the scene
-
Hey,
After some time playing around, I ended up with this.
My Card class inherits QGraphicsPixmapItem. When deck is created and all the cards get their suits and values, they also get a corresponding picture using setPixmap function. Then I call shuffle and deal functions, the later looks something like this (a loop here to put the cards onto the scene) :
@
QGraphicsPixmapItem *item = new QGraphicsPixmapItem();item->setPixmap(QPixmap(card[i].pixmap()));
scene.addItem(item);
@I'm happy with the result so far, but it's not tested fully yet, so don't know if it's going to do exactly what I want.
Please let me know your thoughts.
Thanks!
EDIT:
Although now I think after the cards are put on the screen, they become simply pictures and are not associated with Card class anymore because of this
@ item->setPixmap(QPixmap(card[i].pixmap())); @Any thoughts on this?
-
You could make a subclass of QGraphicsPixmapItem and make it your card e.g. add getters for suit/rank to that subclass
-
Pardon my ignorance (I'm still very new to C++), but when you say 'make a subclass of QGraphicsPixmapItem' is it
@ class Card : public QGraphicsPixmapItem @
or the other way around? And what is a difference? I tried the first one and it didn't work the way I wanted to, or I did something wrong. By the way, my original Card class has its setter and getter methods.Thanks a lot for your help!
-
Yes, it's that.
What didn't work the way you wanted ?
-
Oh, sorry, I simply forgot to pass my Card object as a reference to addItem, it seems to be working now.
Ok, so I've got a couple of cards on the screen now and a mouse press event, that looks like this:
@
void Card::mousePressEvent(QGraphicsSceneMouseEvent *Card)
{
qDebug() << "I clicked a card ";
}
@This is only for testing at the moment. Now, when I click on a Card, I'd like qDebug() to output its suit and value. As I mentioned before, I do have getter methods for that, but not too sure where to go from here.
Again, much appreciated, SGaist!
-
It should rather be:
@
void Card::mousePressEvent(QGraphicsSceneMouseEvent *event) <- here
{
qDebug() << "I clicked a card ";
}@ -
You're welcome !
Don't hesitate to take some times to look at Qt's documentation examples. They might contain a lot of stuff you can reuse/get inspiration from for your application
Happy coding !
-
Hi,
Not too happy about coming back to the same thread, but I'm facing an issue.
@
class Card : public QGraphicsPixmapItem
@This line generates a C2248 error, saying I cannot access private member in a QGraphicsPixmapItem class.
I have only a slight idea as to why this happens, but unfortunately no clue what to do about it. Funny thing is, it compiled no problem before, although I haven't made any changes to the Card class or any other relevant part of the code..
-
What's the exact error you are getting ?
-
error C2248: 'QGraphicsPixmapItem::QGraphicsPixmapItem' : cannot access private member declared in class 'QGraphicsPixmapItem'
error C2248: 'QGraphicsPixmapItem::operator =' : cannot access private member declared in class 'QGraphicsPixmapItem'
And that's compiler's output:
1> c:\qt\5.2.1\msvc2012\include\qtwidgets\qgraphicsitem.h(866) : see declaration of 'QGraphicsPixmapItem::QGraphicsPixmapItem'
1> c:\qt\5.2.1\msvc2012\include\qtwidgets\qgraphicsitem.h(822) : see declaration of 'QGraphicsPixmapItem'
1> This diagnostic occurred in the compiler generated function 'Card::Card(const Card &)'and
'Card &Card::operator =(const Card &)'
-
What you are doing is risky, you've got the error because the base class (QGraphicsPixmapItem ) explicitly forbids the copy.
If you look at QGraphicsScene you see that you addItem takes a pointer to an item
-
Just allocate your Cards on the heap and not on the stack
-
Because somewhere in your code you are doing a copy. Are you using a QList<Card> or something similar ?
-
I saw your reply in my other post and I indeed did do copies by using vector. I'm changing my code around now.
Check this line @ vector<Card*> * player = new vector<Card*>; @ Is it correct? And if so, how do I iterate through player? I'm getting a little confused with all these pointers.