Results 1 to 3 of 3

Thread: update QGraphicsPolygonItem qbrush in mousePressEvent method

  1. #1
    Join Date
    Oct 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default update QGraphicsPolygonItem qbrush in mousePressEvent method

    Hi.

    I'm trying to change the brush on an QGraphicsItem (More precise my own class that inherits from QGraphicsPolygonItem).

    My reimplementation of mousePressEvent looks like this

    Qt Code:
    1. void BlobItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
    2. {
    3. std::cout << "label: " << _label->label << endl;
    4. if (!_label->label.compare("a")) {
    5. QBrush brush;
    6. QColor color("brown");
    7. brush.setColor(color);
    8. setBrush(brush);
    9. _label->label = "b";
    10. this->setZValue(parentItem()->zValue() + 1);
    11. } else if (!_label->label.compare("b")) {
    12. QBrush brush;
    13. QColor color("black");
    14. brush.setColor(color);
    15. setBrush(brush);
    16. _label->label = "a";
    17. this->setZValue(parentItem()->zValue() + 1);
    18. }
    19. update(boundingRect());
    20. QGraphicsPolygonItem::mousePressEvent(event);
    21. event->ignore();
    22. }
    To copy to clipboard, switch view to plain text mode 

    I can see that each time I press the mouse button on an item the label toggles between 'a' and 'b'. My items originally have a brush with a blue color therefore they are initially painted with a blue interior, but when I click on them not only does the color not change to the one specified in the if statements, the original blue color disappears and the background (the parent item) gets visible.

    Can someone see what I'm doing wrong?

    Any help will greatly appreciated

    Regards,
    Thomas

  2. #2
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: update QGraphicsPolygonItem qbrush in mousePressEvent method

    Hi,

    Maybe you need to set the style of the new brush to solid:

    brush.setStyle(Qt::SolidPattern);

  3. #3
    Join Date
    Oct 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: update QGraphicsPolygonItem qbrush in mousePressEvent method

    thank you so much... It has been bothering me a while.
    Now it works as expected..

Similar Threads

  1. QGraphicsPolygonItem with holes
    By natnan in forum Qt Programming
    Replies: 0
    Last Post: 2nd August 2010, 09:44
  2. matrix for QBrush
    By navi1084 in forum Qt Programming
    Replies: 5
    Last Post: 5th February 2010, 11:27
  3. I need help in QGraphicsPolygonItem
    By c_srikanth1984 in forum Qt Programming
    Replies: 0
    Last Post: 15th April 2009, 10:53
  4. QGraphicsPolygonItem Returning line to start
    By Terabyte in forum Qt Programming
    Replies: 1
    Last Post: 29th December 2008, 07:22
  5. QPaintEngine and QGraphicsPolygonItem
    By magland in forum Qt Programming
    Replies: 1
    Last Post: 19th August 2008, 16:03

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.