Results 1 to 2 of 2

Thread: Painting selection grips for a QGraphicsRectItem

  1. #1
    Join Date
    Nov 2010
    Posts
    77
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Painting selection grips for a QGraphicsRectItem

    I want to paint selection grips on a QGraphicsRectItem. Currently, I do this by overriding the paint event.



    Qt Code:
    1. void GraphicNode::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
    2. drawShape(painter);
    3.  
    4. if (this->isSelected())
    5. drawSelectionGrips(painter);
    6.  
    7. if (this->associatedNode)
    8. drawLabel(painter);
    9. }
    10.  
    11. void GraphicNode::drawSelectionGrips(QPainter *painter) {
    12. painter->setPen(Qt::SolidLine);
    13. painter->setPen(Qt::black);
    14. painter->setBrush(Qt::black);
    15.  
    16. painter->drawRect(this->boundingRect().x(), this->boundingRect().y(), GRIPSIZE, GRIPSIZE);
    17.  
    18. int topRightX = this->boundingRect().x() + this->boundingRect().width() - GRIPSIZE - SHADOWSIZE;
    19. painter->drawRect(topRightX, this->boundingRect().y(), GRIPSIZE, GRIPSIZE);
    20.  
    21. int bottomLeftY = this->boundingRect().y() + this->boundingRect().height() - GRIPSIZE - SHADOWSIZE;
    22. painter->drawRect(this->boundingRect().x(), bottomLeftY, GRIPSIZE, GRIPSIZE);
    23.  
    24. int bottomRightX = this->boundingRect().x() + this->boundingRect().width() - SHADOWSIZE;
    25. int bottomRightY = this->boundingRect().y() + this->boundingRect().height() - SHADOWSIZE;
    26. painter->drawRect(bottomRightX - GRIPSIZE, bottomRightY - GRIPSIZE, GRIPSIZE, GRIPSIZE);
    27.  
    28. painter->setPen(Qt::DotLine);
    29. painter->setBrush(Qt::transparent);
    30. painter->drawRect(this->boundingRect().x(), this->boundingRect().y(), this->boundingRect().width() - SHADOWSIZE, this->boundingRect().height() - SHADOWSIZE);
    31. }
    To copy to clipboard, switch view to plain text mode 

    I want the cursor to change when the user hovers over the selection grips, so I was thinking I'd convert them into four QGraphicsRectItems and use setCursor to modify the cursor.

    In the constructor:
    Qt Code:
    1. this->topLeftGrip = new QGraphicsRectItem();
    2. this->topLeftGrip->setCursor(Qt::SizeFDiagCursor);
    3. this->topLeftGrip->setPen(QPen(Qt::black));
    4. this->topLeftGrip->setBrush(QBrush(Qt::black));
    5. this->topRightGrip = new QGraphicsRectItem();
    6. this->topRightGrip->setCursor(Qt::SizeBDiagCursor);
    7. this->topRightGrip->setPen(QPen(Qt::black));
    8. this->topRightGrip->setBrush(QBrush(Qt::black));
    9. this->bottomLeftGrip = new QGraphicsRectItem();
    10. this->bottomLeftGrip->setCursor(Qt::SizeFDiagCursor);
    11. this->bottomLeftGrip->setPen(QPen(Qt::black));
    12. this->bottomLeftGrip->setBrush(QBrush(Qt::black));
    13. this->bottomRightGrip = new QGraphicsRectItem();
    14. this->bottomRightGrip->setCursor(Qt::SizeBDiagCursor);
    15. this->bottomRightGrip->setPen(QPen(Qt::black));
    16. this->bottomRightGrip->setBrush(QBrush(Qt::black));
    To copy to clipboard, switch view to plain text mode 

    In the selection drawing function:
    Qt Code:
    1. this->topLeftGrip->setRect(this->boundingRect().x(), this->boundingRect().y(), GRIPSIZE, GRIPSIZE);
    2. this->topLeftGrip->setVisible(true);
    3.  
    4. int topRightX = this->boundingRect().x() + this->boundingRect().width();
    5. this->topRightGrip->setRect(topRightX, this->boundingRect().y(), GRIPSIZE, GRIPSIZE);
    6. this->topRightGrip->setVisible(true);
    7.  
    8. int bottomLeftY = this->boundingRect().y() + this->boundingRect().height() - GRIPSIZE - SHADOWSIZE;
    9. this->bottomLeftGrip->setRect(this->boundingRect().x(), bottomLeftY, GRIPSIZE, GRIPSIZE);
    10. this->bottomLeftGrip->setVisible(true);
    11.  
    12. int bottomRightX = this->boundingRect().x() + this->boundingRect().width() - SHADOWSIZE;
    13. int bottomRightY = this->boundingRect().y() + this->boundingRect().height() - SHADOWSIZE;
    14. this->bottomRightGrip->setRect(bottomRightX - GRIPSIZE, bottomRightY - GRIPSIZE, GRIPSIZE, GRIPSIZE);
    15. this->bottomRightGrip->setVisible(true);
    To copy to clipboard, switch view to plain text mode 

    I don't see any selection grips when I use the QGraphicsRectItem approach. Did I forget something important? Is there a better way to draw the selection grips? (Keep in mind that my object also uses drop shadow, so I may not be able to use Qt's built-in selection features.)

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Painting selection grips for a QGraphicsRectItem

    Why don't you change the mouse cursor of your item in its hoverMoveEvent when the cursor is over one of the grips?

    In order to recieve mouseMoveEvents without a pressed button you will need to activate mousetracking on the view and setAcceptHoverEvents(true) on the item.

    HIH

    Johannes
    Last edited by JohannesMunk; 1st April 2011 at 19:14.

  3. The following user says thank you to JohannesMunk for this useful post:

    blooglet (1st April 2011)

Similar Threads

  1. Replies: 0
    Last Post: 11th November 2010, 22:36
  2. QGraphicsRectItem signal
    By kovacadam in forum Qt Programming
    Replies: 5
    Last Post: 1st October 2010, 14:32
  3. QGraphicsRectItem
    By c_srikanth1984 in forum Qt Programming
    Replies: 1
    Last Post: 29th July 2009, 09:04
  4. How can I use QGraphicsRectItem?
    By learning_qt in forum Qt Programming
    Replies: 3
    Last Post: 14th January 2009, 10:00
  5. QListWidget selection custom painting problem
    By xeento in forum Qt Programming
    Replies: 3
    Last Post: 27th November 2008, 09:49

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.