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.)