当前位置:   article > 正文

QGraphicsItem画空心图形_qt paint 画实心矩形

qt paint 画实心矩形

用QGraphicsItem类族画出的图形通常都是一个区域(实心的),比如画个圆或者画个矩形。那如果想画个矩形框或者圆形框呢?可以用如下方法,直接上代码

头文件

  1. #include <QGraphicsScene>
  2. #include <QGraphicsRectItem>
  3. #include <QGraphicsPathItem>
  4. #include <QGraphicsEllipseItem>
  5. namespace Ui {
  6. class Widget;
  7. }
  8. class MyRectItem : public QGraphicsRectItem
  9. {
  10. public:
  11. MyRectItem(const QRectF &rect, QGraphicsItem *parent = 0);
  12. MyRectItem(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent = 0);
  13. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
  14. QPainterPath shape() const;
  15. protected:
  16. void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
  17. void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
  18. private:
  19. bool isHovered;
  20. };
  21. class MyEllipseItem : public QGraphicsEllipseItem
  22. {
  23. public:
  24. MyEllipseItem(const QRectF &rect, QGraphicsItem *parent = 0);
  25. MyEllipseItem(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent = 0);
  26. QRectF boundingRect() const;
  27. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
  28. QPainterPath shape() const;
  29. protected:
  30. void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
  31. void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
  32. private:
  33. bool isHovered;
  34. QRectF rect;
  35. };

源文件

  1. #include <QPainterPath>
  2. #include <QPainterPathStroker>
  3. MyRectItem::MyRectItem(const QRectF &rect, QGraphicsItem *parent)
  4. : QGraphicsRectItem(rect, parent)
  5. {
  6. this->isHovered = false;
  7. setFlag(QGraphicsItem::ItemIsSelectable);
  8. setAcceptHoverEvents(true);
  9. }
  10. MyRectItem::MyRectItem(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent)
  11. : QGraphicsRectItem(x, y, w, h, parent)
  12. {
  13. this->isHovered = false;
  14. setFlag(QGraphicsItem::ItemIsSelectable);
  15. setAcceptHoverEvents(true);
  16. }
  17. void MyRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  18. {
  19. QPen pen;
  20. if (this->isSelected())
  21. {
  22. pen.setColor(Qt::red);
  23. }
  24. else
  25. {
  26. pen.setColor(Qt::green);
  27. }
  28. if (this->isHovered)
  29. {
  30. pen.setWidth(4);
  31. }
  32. else
  33. {
  34. pen.setWidth(2);
  35. }
  36. painter->setPen(pen);
  37. painter->drawRect(this->boundingRect());
  38. }
  39. QPainterPath MyRectItem::shape() const
  40. {
  41. QPainterPath temp;
  42. temp.addRect(this->boundingRect());
  43. QPainterPathStroker pathStroker;
  44. QPainterPath path = pathStroker.createStroke(temp);
  45. return path;
  46. }
  47. void MyRectItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
  48. {
  49. this->isHovered = true;
  50. prepareGeometryChange();
  51. }
  52. void MyRectItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
  53. {
  54. this->isHovered = false;
  55. prepareGeometryChange();
  56. }
  57. MyEllipseItem::MyEllipseItem(const QRectF &rect, QGraphicsItem *parent)
  58. : QGraphicsEllipseItem(rect, parent)
  59. {
  60. this->isHovered = false;
  61. setFlag(QGraphicsItem::ItemIsSelectable);
  62. setAcceptHoverEvents(true);
  63. this->rect = rect;
  64. }
  65. MyEllipseItem::MyEllipseItem(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent)
  66. : QGraphicsEllipseItem(x, y, w, h, parent)
  67. {
  68. this->isHovered = false;
  69. setFlag(QGraphicsItem::ItemIsSelectable);
  70. setAcceptHoverEvents(true);
  71. this->rect.setX(x);
  72. this->rect.setY(y);
  73. this->rect.setWidth(w);
  74. this->rect.setHeight(h);
  75. }
  76. QRectF MyEllipseItem::boundingRect() const
  77. {
  78. return this->rect;
  79. }
  80. void MyEllipseItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  81. {
  82. QPen pen;
  83. if (this->isSelected())
  84. {
  85. pen.setColor(Qt::red);
  86. }
  87. else
  88. {
  89. pen.setColor(Qt::green);
  90. }
  91. if (this->isHovered)
  92. {
  93. pen.setWidth(4);
  94. }
  95. else
  96. {
  97. pen.setWidth(2);
  98. }
  99. painter->setPen(pen);
  100. painter->drawEllipse(this->rect);
  101. }
  102. QPainterPath MyEllipseItem::shape() const
  103. {
  104. QPainterPath temp;
  105. temp.addEllipse(this->boundingRect());
  106. QPainterPathStroker pathStroker;
  107. QPainterPath path = pathStroker.createStroke(temp);
  108. return path;
  109. }
  110. void MyEllipseItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
  111. {
  112. this->isHovered = true;
  113. prepareGeometryChange();
  114. }
  115. void MyEllipseItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
  116. {
  117. this->isHovered = false;
  118. prepareGeometryChange();
  119. }

程序中包括一个矩形类和一个椭圆形类,选中会变色,悬停会变粗。

通过实现shape函数,使用QPainterPathStroker来画图形的外框,这样画出的图像就是空心的。

 

测试代码:

在scene上添加图形:

  1. MyRectItem *itemRect = new MyRectItem(10, 10, 100, 100);
  2. scene->addItem(itemRect);
  3. MyRectItem *item2 = new MyRectItem( 20, 20, 120, 120);
  4. scene->addItem(item2);
  5. MyEllipseItem *item3 = new MyEllipseItem(30, 30, 50, 50);
  6. scene->addItem(item3);
  7. MyEllipseItem *item4 = new MyEllipseItem(50, 50, 100, 80);
  8. scene->addItem(item4);

如图所示,鼠标划过时,框内图形可以选中也可以点击。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/95134
推荐阅读
相关标签
  

闽ICP备14008679号