Results 1 to 10 of 10

Thread: QGraphicsItem paint not triggered

  1. #1
    Join Date
    Feb 2011
    Posts
    20
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QGraphicsItem paint not triggered

    If I add my subclassed QGraphicsItem before I add anything else to the scene everything works fine. But if I add something else before then the paint method is never triggered in my new QGraphicsItem. Can somebody explain what I am missing?


    Qt Code:
    1. MyScene::MyScene(QObject *parent)
    2. : QGraphicsScene(parent), currentDrawItem(NULL), bDraw(false)
    3. {
    4. void MyScene::toggleSymbols()
    5. {
    6. if(bSymbols)
    7. {
    8. qDebug("false");
    9. bSymbols = false;
    10. }
    11. else
    12. {
    13. qDebug("true");
    14. bSymbols = true;
    15. symbols = new itemSymbols(); //Subclassed QGraphicsItem
    16. QPointF centerPos = QPointF(this->sceneRect().width()/2, this->sceneRect().height()/2);
    17. symbols->setCenterPos(centerPos);
    18. addItem(symbols);
    19. }
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. itemSymbols::itemSymbols() : QGraphicsItem()
    2. {
    3. void itemSymbols::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    4. {
    5. Debug("itemSymbols paint()");
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsItem paint not triggered

    How did you implement boundingRect() for the item?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2011
    Posts
    20
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsItem paint not triggered

    I've modified the code a bit know but still the same result.

    In MyScene.cpp I create the Item and set it´s size like this
    Qt Code:
    1. symbols = new itemSymbols();
    2. QSizeF size(this->sceneRect().width()*0.05,this->sceneRect().height()*0.07);
    3. QPointF centerPos = QPointF(this->sceneRect().center());
    4. symbols->setSize(centerPos, size);
    5. addItem(symbols);
    To copy to clipboard, switch view to plain text mode 

    And then in the item itself I do this

    Qt Code:
    1. void itemSymbols::setSize(QPointF pos, QSizeF size)
    2. {
    3. QRectF test(pos size);
    4. bBox = test;
    5. }
    6. QRectF itemSymbols::boundingRect() const
    7. {
    8. return bBox;
    9. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsItem paint not triggered

    No, this is wrong. If you modify the boundingRect value, you need to inform Graphics View about it by calling QGraphicsItem::prepareGeometryChange().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2011
    Posts
    20
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsItem paint not triggered

    Oh ok. Like this?

    Qt Code:
    1. void itemSymbols::setSize(QPointF pos, QSizeF size)
    2. {
    3. centerPos = pos;
    4. QRectF test(centerPos,size);
    5. QGraphicsItem::prepareGeometryChange();
    6. bBox = test;
    7. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsItem paint not triggered

    Swap #5 and #6, just in case. And of course make sure you paint within the rectangle returned by boundingRect.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Feb 2011
    Posts
    20
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsItem paint not triggered

    Ok thanks alot wysota. It still isn't working but now the paint method is triggered so hopefully it's not that hard no solve.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsItem paint not triggered

    What isn't working? From what I see you're not painting anything. Furthermore it seems your item should probably be derived from QGraphicsRectItem instead of QGraphicsItem.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Feb 2011
    Posts
    20
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsItem paint not triggered

    This is my paint method. What I'm really trying to do here is a movable legend. As you can see the pen size is way off but any less than 200 is not visible so my guess is that I've mixed up the maybe the scene's coordinatesystem with the item's?

    Qt Code:
    1. void itemSymbols::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    2. {
    3. painter->setRenderHint(QPainter::Antialiasing);
    4. qDebug("itemSymbols paint()");
    5.  
    6. bBox.setHeight(height);
    7. bBox.setWidth(width);
    8.  
    9. QPointF header;
    10. header.setX(width/2);
    11. header.setY(height*0.05);
    12.  
    13. qreal newHeight = header.y();
    14.  
    15. QFont font;
    16. font.setBold(true);
    17. painter->setFont(font);
    18.  
    19. QPen pen;
    20. pen.setWidth(200);
    21. painter->setPen(pen);
    22.  
    23. painter->drawText(header,"Symbols");
    24.  
    25. qreal iRows = (height-newHeight)/15;
    26. qreal h = iRows;
    27.  
    28. qreal iCol = width/4;
    29. qreal c = iCol;
    30.  
    31. font.setBold(false);
    32. painter->setFont(font);
    33.  
    34. QFont font2;
    35. font2.setPointSizeF(4);
    36. painter->setFont(font2);
    37.  
    38. for(int i = 0;i<vPics.size();i++)
    39. {
    40. p.setX(10.0);
    41. p.setY(h+10);
    42.  
    43. QPointF p2;
    44. p2.setX(60.0);
    45. p2.setY(h);
    46.  
    47. h = h + iRows;
    48. c = c + iCol;
    49.  
    50. QPixmap pixmap(vPics.at(i));
    51. QRectF d = QRectF(p.x(),p.y(),28,28);
    52. //painter->drawRect(d);
    53. //painter->drawPixmap(p,pixmap);
    54.  
    55. const char *chTemp;
    56. chTemp = vDesc.at(i).toAscii();
    57. QRectF t = QRectF(p2.x(), p.y(),120,30);
    58. //painter->drawRect(t);
    59. //painter->drawText(QRectF(p2.x(), p.y(),120,30), Qt::AlignCenter, QObject::tr(chTemp));
    60. }
    61.  
    62. qDebug("Drawing a box at pos %f %f", bBox.topLeft().x(), bBox.topLeft().y());
    63. painter->drawRect(bBox);
    64. painter->drawPoint(centerPos);
    65. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsItem paint not triggered

    I don't know what width and height are but changing the bounding box in the paint routine is definitely a bad idea. The method is called paint() and not paintAndUpdateBoundingBox(). Not knowing the scene size and the transformation matrix of the view I'm unable to say anything about the pen size.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 3
    Last Post: 28th September 2010, 10:48
  2. Replies: 4
    Last Post: 30th August 2010, 14:37
  3. OpenGL functions in QGraphicsItem::paint
    By Lendrick in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2010, 17:30
  4. Qgraphicsitem parent/child paint problem.
    By repka3 in forum Qt Programming
    Replies: 1
    Last Post: 24th July 2009, 22:03
  5. Paint QGraphicsItem problem
    By dreamer in forum Qt Programming
    Replies: 3
    Last Post: 23rd June 2008, 18:18

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.