Results 1 to 11 of 11

Thread: Wrong repainting on resize.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2011
    Posts
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Symbian S60 Maemo/MeeGo

    Default Wrong repainting on resize.

    Hi Everybody, I'm having a problem with repainting a scene, I don't know what I'm doing wrong, so here I am.

    So, summary. I have a board witch is a QGraphicsView and then I have QGraphicsItem from pixmap, where I paint pixmap to painter in paint function of QGraphicsItem.

    Back to QGraphicsView I set a new scene and add items. Pixmaps are rendered from svg creating new pixmap, passing it to painter and paint it using data from svg.

    Then when I resize a window (in resizeEvent) I reposition items and set new size of board.
    Result is wrong, see attachment.

    Code is as follows:
    Qt Code:
    1. kMancalaBoard::kMancalaBoard ( QSize dim = QSize(850, 475), QWidget *parent = 0 )
    2. : QGraphicsView (parent) {
    3. m_scene = new QGraphicsScene();
    4. m_renderer = new kMancalaRenderer(this);
    5.  
    6. setScene ( m_scene );
    7.  
    8. setSize(dim);
    9. addItems();
    10. }
    11.  
    12. void kMancalaBoard::positionItems() {
    13. qDebug("positionItems(): Positioning items");
    14.  
    15. qreal height = sceneRect().height();
    16. qreal width = sceneRect().width();
    17. qreal middle = height/2;
    18.  
    19. int treasury_width = GAP_TREASURY_WIDTH+BORDER;
    20. int gaps_space = width - 2*treasury_width;
    21. int gap_offset = (gaps_space-(6*GAP_WIDTH))/7;
    22. int left_offset = treasury_width + gap_offset;
    23.  
    24. treasury[0]->setPos(BORDER, middle-GAP_TREASURY_HEIGHT/2);
    25. treasury[1]->setPos(width-BORDER-GAP_TREASURY_WIDTH, middle-GAP_TREASURY_HEIGHT/2);
    26.  
    27. for(int i = 0; i != 6; i++) {
    28. hole[i]->setPos(left_offset+(i*(GAP_WIDTH+gap_offset)), height*0.3);
    29. hole[i+6]->setPos(left_offset+(i*(GAP_WIDTH+gap_offset)), height*0.7-GAP_HEIGHT);
    30. }
    31. }
    32.  
    33. void kMancalaBoard::addItems() {
    34. qDebug("addItems(): Adding items");
    35.  
    36. QPixmap bean = m_renderer->renderPixmap("bean", QSize(GAP_WIDTH, GAP_HEIGHT));
    37. for(int i = 0; i != 2; i++) {
    38. treasury[i] = new kMancalaBoardGap(
    39. m_renderer->renderPixmap("treasury", QSize(GAP_TREASURY_WIDTH, GAP_TREASURY_HEIGHT)),
    40. bean
    41. );
    42. m_scene->addItem(treasury[i]);
    43. }
    44.  
    45. for(int i =0; i != 12; i++) {
    46. hole[i] = new kMancalaBoardGap(
    47. m_renderer->renderPixmap("hole", QSize(GAP_WIDTH, GAP_HEIGHT)),
    48. bean
    49. );
    50. m_scene->addItem(hole[i]);
    51. }
    52. }
    53.  
    54. kMancalaBoard::~kMancalaBoard() {
    55. }
    56.  
    57. void kMancalaBoard::drawBackground(QPainter *painter, const QRectF &rect) {
    58. qDebug("drawBackground(): Drawing background");
    59. if ( m_renderer->elementExists ( QString ( "board" ) ) ) {
    60. qDebug ("drawBackground(): -> Rendering" );
    61. m_renderer->render(painter, QString ( "background" ), rect);
    62. m_renderer->render(painter, QString ( "board" ), rect);
    63. } else {
    64. qDebug ("drawBackground(): -> No background found" );
    65. }
    66.  
    67. QGraphicsView::drawBackground(painter, rect);
    68. }
    69.  
    70. void kMancalaBoard::resizeEvent(QResizeEvent *event) {
    71. qDebug("resizeEvent(): Resize event");
    72. QSize s = size();
    73.  
    74. setSize(s);
    75. positionItems();
    76.  
    77. qDebug("resizeEvent(): -> Scene size now %d x %d", s.width(), s.height());
    78. }
    79.  
    80. void kMancalaBoard::setSize(QSize s) {
    81. qDebug("setSize(): Setting size");
    82. qDebug("setSize(): -> Setting scene to %d x %d", s.width(), s.height());
    83. m_scene->setSceneRect(QRectF(0,0,s.width()-10,s.height()-10));
    84.  
    85. QRectF scene_rect = m_scene->sceneRect();
    86. qDebug("setSize(): -> Scene rectangle: x: %lf, y: %lf, width: %lf, height: %lf", scene_rect.x(), scene_rect.y(), scene_rect.width(), scene_rect.height());
    87. }
    To copy to clipboard, switch view to plain text mode 

    and kMancalaBoardGap (QGraphicsItem)
    Qt Code:
    1. kMancalaBoardGap::kMancalaBoardGap(QPixmap pGap, QPixmap pBean)
    2. pixmapBean = pBean;
    3. pixmapGap = pGap;
    4.  
    5. beans = 0;
    6. }
    7.  
    8. kMancalaBoardGap::~kMancalaBoardGap() {
    9. }
    10.  
    11. QRectF kMancalaBoardGap::boundingRect() const {
    12. return QRectF(0, 0, pixmapGap.width(), pixmapGap.height());
    13. }
    14.  
    15. void kMancalaBoardGap::paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
    16. QWidget* widget) {
    17.  
    18. qDebug("kMancalaBoardGap::paint(): Painting gap");
    19. //painter->fillRect(0,0,pixmapGap.width(), pixmapGap.height(), Qt::transparent);
    20. painter->drawPixmap(0,0,pixmapGap);
    21. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

Similar Threads

  1. Repainting a QGraphicsView
    By Luc4 in forum Qt Programming
    Replies: 8
    Last Post: 29th April 2010, 14:09
  2. QGraphicsItem not repainting
    By eijnuhs in forum Qt Programming
    Replies: 3
    Last Post: 20th September 2008, 08:54
  3. Repainting widget
    By fear in forum Qt Programming
    Replies: 3
    Last Post: 26th March 2008, 08:37
  4. Replies: 2
    Last Post: 22nd January 2008, 16:10
  5. GraphicsScene Background repainting
    By manojmka in forum Qt Programming
    Replies: 3
    Last Post: 18th December 2007, 10:33

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
  •  
Qt is a trademark of The Qt Company.