Important: Please read the Qt Code of Conduct -

[solved] deal cards and add to the scene

  • Hey guys,

    I can't seem to figure out the best way to do this. Here is what I've got (to put it simply):

    A Card class that has its suit and rank;
    A Deck class made of 32 Cards. In here a Card is assigned a suit and a rank using Card's setter function. Then the Deck is shuffled and Cards dealt to 3 players (using 3 vectors, 10 cards each).

    At this stage, Cards are only sets of numbers, i.e. 1/5, 3/4 etc. The first number is a suit (1-4) and the second is a rank (1-8). And that's where I get stuck. How do I incorporate Qt so that every card is assigned its corresponding GIF image (let's say 1/1 would be seven of hearts, 2/2 - eight of diamonds etc) and put in a scene?

  • Lifetime Qt Champion


    You could use a QMap<QPair<int, int>, QPixmap> that will be your card deck.

    Then copy the keys (QList<QPair<int, int>> cards = myMap.keys() and run std::random_shuffle on them.

    From there you can create your scene getting the pixmap base on your randomized cards.

    Hope it helps.

  • Thank you for your input, SGaist. I'll give it a try and see how far I get with this. Will be back with an update. Meanwhile, if anyone else has any other ideas, that would be greatly appreciated!


  • 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();


    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.



    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?

  • Sorry to bombard this thread with questions, but how could I use
    @ scene.addItem(item); @ item being my Card object? Since all of them already are QGraphicsPixmapItems and have Qpixmaps assigned. I think that would help me a lot.

  • Lifetime Qt Champion

    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!

  • Lifetime Qt Champion

    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!

  • Lifetime Qt Champion

    It should rather be:

    void Card::mousePressEvent(QGraphicsSceneMouseEvent *event) <- here
    qDebug() << "I clicked a card ";

  • There's still heaps of work left to be done, but you, sir, got me helluva closer :)

    Thank you!

  • Lifetime Qt Champion

    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..

  • Lifetime Qt Champion

    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 &)'


    'Card &Card::operator =(const Card &)'

  • I can't believe I did it (given my almost non-existent experience), but I finally got rid of the errors by providing my own class copy and assignment operator constructors. Please keep tuned though, will see where it leads me to.. :)

  • Lifetime Qt Champion

    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

  • I see what you're saying. I actually may be having problems because of that at the moment (not sure yet though). How would you go about this issue then? Quite frankly, I still don't know which which part of my program causes the error.

  • Lifetime Qt Champion

    Just allocate your Cards on the heap and not on the stack

  • I tried that, although I still can't remove copy and assignment operator constructors without getting the same error..

  • Lifetime Qt Champion

    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.

  • Lifetime Qt Champion

    Just use vector<Card *> player, there's no need of a pointer for the vector

  • @
    for (vector<Card*>::iterator iter = player.begin();
    iter != player.end(); ++iter)
    qDebug() << iter->GetSuit()

    How do I call GetSuit() function? This is obviously incorrect

  • Lifetime Qt Champion



    You should be good

  • Tried it before I asked :) But I get an error: "expression must have pointer-to-class type"

  • Lifetime Qt Champion

    Sorry, there was a mistake in my code example

  • Ah, simple as that. Works like a charm :)

  • Lifetime Qt Champion

    Yes, sometimes it's a simple thing :) But can be hard to track down :-D

Log in to reply