[solved] vectors and QGraphicsPixmapItem



  • Hi everyone,

    I'm facing yet another issue which I'm struggling with.

    I have a vector, vector<Card> player, where Card is a class with its suit, rank and inherits QGraphicsPixmapItem. Let's say the vector has 5 Card items with their suits, ranks and pixmaps set.

    @
    for (vector<Card>::iterator iter = player->begin();
    iter != player->end(); ++iter)
    {
    qDebug() << iter->GetSuit() << iter->GetRank();
    scene->addItem(iter);
    }
    @

    Here, I successfully get suits and ranks, but cannot add object to the scene. I get en error: "no suitable conversion function from "std::_Vector_iterator<std::_Vector_val<std::_Simple_types<Card>>>" to "QGraphicsItem *" exists"

    If I do something like this:
    @
    Card * card = new Card;
    card->SetCard(1,1);
    card->setPixmap(QPixmap("/card set/h7.gif"));
    scene->addItem(card);
    @
    it works fine.

    I don't understand what I do wrong.


  • Lifetime Qt Champion

    Hi,

    vector<Card> implies that you can copy Card which is not the case since it's a QGraphicsPixmapItem. You can however use vector<Card *> or QVector<Card *>



  • Got it.

    So just to double check with you (because I'm about to rewrite quite a few bits of my code), when I use something like Card card, vector<card>, card = card2 etc - all of this would create a copy, which in turn generates that error that I'm getting?


  • Lifetime Qt Champion

    That's it, you're right



  • Much appreciated!

    And I came to realize how poorly written my code is..


  • Lifetime Qt Champion

    Learning curve ;-)

    Time to clean and refactor, and it's something you'll do several times in your carrier :)

    If this answers your question, please update the thread title to solved so other forum users may know a solution has been found :)



  • I know, and I do enjoy learning these things. I just wish I would spend a little less time coping with all sorts of errors and more time on actual coding :) But I guess that's a part of it too.

    Well, just finished rewriting the code. All I have now is a bucketload of pointers and references and got rid of those errors I previously had. All thanks to you!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.