Results 1 to 5 of 5

Thread: Calling setPos() on custom QGraphicsPixmapItem doesn't update item in GraphicsScene

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2020
    Posts
    7
    Thanks
    1
    Qt products
    Qt5

    Default Calling setPos() on custom QGraphicsPixmapItem doesn't update item in GraphicsScene

    Hello, I'm working on a little poker game and I am having a graphical glitch I would like to understand better and fix. For my "Card" class I subclassed QGraphicsPixmap items and added a 52-card "deck" to my QGraphicsScene.

    During runtime, I click on a button to deal the cards which then calls setPos() on the first three to simulate a flop.

    The problem is, the cards don't appear to fully draw themselves right away (see photos). When I resize the window suddenly they pop into full view. The QGraphicsView is not being updated when a card position changes.

    I tested calling setPos() on my background image which is a regular QGraphicsPixmap item and it moves properly and immediately updates the entire QGraphicsScene, so I'm thinking it has to be some error I've made while creating my "Card" class, but I'm not sure what I'm doing wrong.

    pokerTable.cpp (relevant portion)
    Qt Code:
    1. void PokerTable::dealFlop()
    2. {
    3. m_deck->burnCard();
    4.  
    5. m_flopCard1 = m_deck->dealCard(); // dealCard() returns a pointer to a Card, stored in these data members
    6. m_flopCard2 = m_deck->dealCard();
    7. m_flopCard3 = m_deck->dealCard();
    8.  
    9. m_flopCard1->setPos(m_flopCard1Position); //QPointF positions
    10. m_flopCard2->setPos(m_flopCard2Position);
    11. m_flopCard3->setPos(m_flopCard3Position);
    12. }
    To copy to clipboard, switch view to plain text mode 

    Card.h
    Qt Code:
    1. #ifndef CARD_H
    2. #define CARD_H
    3.  
    4. #include <QGraphicsPixmapItem>
    5. #include <QLabel>
    6.  
    7. class Card : public QGraphicsPixmapItem
    8. {
    9. public:
    10. enum class Rank
    11. {
    12. TWO,
    13. THREE,
    14. FOUR,
    15. FIVE,
    16. SIX,
    17. SEVEN,
    18. EIGHT,
    19. NINE,
    20. TEN,
    21. JACK,
    22. QUEEN,
    23. KING,
    24. ACE,
    25. MAX_RANKS
    26. };
    27.  
    28. enum class Suit
    29. {
    30. CLUBS,
    31. DIAMONDS,
    32. HEARTS,
    33. SPADES,
    34. MAX_SUITS
    35. };
    36.  
    37. Card();
    38.  
    39. Card(const Card &card2);
    40.  
    41. Card& operator=(const Card &card);
    42.  
    43. int getCardValue();
    44.  
    45. void setRank(int rank);
    46.  
    47. void setSuit(int suit);
    48.  
    49. void setTexture(QPixmap texture);
    50.  
    51. void setSprite();
    52.  
    53. void printCard();
    54.  
    55. protected:
    56. virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
    57.  
    58. virtual QRectF boundingRect() const override;
    59.  
    60. private:
    61. QPixmap m_texture;
    62. QPointF m_sprite;
    63.  
    64. Rank m_rank;
    65. Suit m_suit;
    66. };
    67.  
    68. #endif // CARD_H
    To copy to clipboard, switch view to plain text mode 

    Card.cpp (relevant portion)
    Qt Code:
    1. #include "Card.h"
    2. #include <QPainter>
    3. #include <iostream>
    4.  
    5. Card::Card()
    6. : m_sprite{ QPointF(0, 0) }
    7. {
    8. setScale(0.2);
    9. setFlag(ItemSendsGeometryChanges, true);
    10. }
    11.  
    12. Card::Card(const Card &card2)
    13. {
    14. m_texture = card2.m_texture;
    15. m_sprite = card2.m_sprite;
    16.  
    17. m_rank = card2.m_rank;
    18. m_suit = card2.m_suit;
    19. }
    20.  
    21. Card& Card::operator=(const Card &card)
    22. {
    23. m_texture = card.m_texture;
    24. m_sprite = card.m_sprite;
    25.  
    26. m_rank = card.m_rank;
    27. m_suit = card.m_suit;
    28.  
    29. return *this;
    30. }
    31.  
    32. void Card::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    33. {
    34. painter->drawPixmap(x(), y(), m_texture, m_sprite.x(), m_sprite.y(), 560, 780);
    35. }
    36.  
    37. QRectF Card::boundingRect() const
    38. {
    39. return QRectF(0, 0, 560, 780);
    40. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 7
    Last Post: 31st October 2010, 12:21
  2. QCursor::setPos() - doesn't work
    By Piskvorkar in forum Qt Programming
    Replies: 0
    Last Post: 21st March 2010, 13:24
  3. GraphicsView/GraphicsScene does not update
    By robicjedi in forum Qt Programming
    Replies: 5
    Last Post: 2nd February 2010, 23:33
  4. Replies: 1
    Last Post: 26th August 2009, 15:47
  5. How to move an item on a GraphicsScene
    By Holy in forum Qt Programming
    Replies: 17
    Last Post: 25th July 2008, 14:40

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.