Results 1 to 11 of 11

Thread: Wrong repainting on resize.

  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

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Wrong repainting on resize.

    Result is wrong, see attachment.
    Please define "wrong".
    As you didn't say what is 'right'.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    Default Re: Wrong repainting on resize.

    I think that on the attached image is obvious what is wrong. The background of gaps is moved and not filled correctly.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Wrong repainting on resize.

    I think that on the attached image is obvious what is wrong.
    Apparently it was not obvious, otherwise I would not have asked.

    Can you show the code for m_renderer->renderPixmap()?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    Default Re: Wrong repainting on resize.

    Sure, it's as follows

    Qt Code:
    1. QPixmap kMancalaRenderer::renderPixmap(QString name, const QSize size) {
    2. QPixmap p(size);
    3. p.fill(Qt::transparent);
    4.  
    5. QPainter painter(&p);
    6.  
    7. if ( !elementExists(name)) {
    8. qErrnoWarning("Element doesn't exist");
    9. return QPixmap();
    10. }
    11.  
    12. render(&painter, name, QRectF(0,0,size.width(), size.height()));
    13. painter.end();
    14.  
    15. return p;
    16. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Wrong repainting on resize.

    Well, you need to resize the loaded image to fit in the new sized QPixmap!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    Default Re: Wrong repainting on resize.

    But the size of a pixmap doesn't change. I'll create a pixmap of some size, then make it transparent and render a svg object over it. And after that pixmap size doesn't change, it's still the same.

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Wrong repainting on resize.

    But if the scene is larger after the resize, then you get holes if your pixmaps wont be able to fill the larger areas they need to fill which is what you get, if I unerstand your screenshort correctly.
    Can you post the "before" screen shot as well?
    And what happens when you resize to a smaller size?

    Why don't you just set your items on a larger item serving as background?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    Default Re: Wrong repainting on resize.

    If I resize if only by 10 px result is what you can see on the first screenshot.

    About the suggestion, do you mean to have the background as another item in back of all the others? Not to paint it to background?
    Attached Images Attached Images

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Wrong repainting on resize.

    Does your background have holes in it?
    Just use one without holes.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    Default Re: Wrong repainting on resize.

    No, it doesn't. The holes are pixmaps added to scene.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.