You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
852 B

  1. #include "myscene.h"
  2. #include <QPainter>
  3. MyScene::MyScene(QObject *parent) : QGraphicsScene(parent)
  4. {
  5. }
  6. void MyScene::drawBackground(QPainter *painter, const QRectF &rect)
  7. {
  8. QGraphicsScene::drawBackground(painter, rect);
  9. // 设置画笔
  10. QPen pen(gridColor);
  11. pen.setWidth(1);
  12. painter->setPen(pen);
  13. // 计算网格线
  14. qreal left = int(rect.left()) - (int(rect.left()) % gridSize);
  15. qreal top = int(rect.top()) - (int(rect.top()) % gridSize);
  16. QVarLengthArray<QLineF, 100> lines;
  17. for (qreal x = left; x < rect.right(); x += gridSize) {
  18. lines.append(QLineF(x, rect.top(), x, rect.bottom()));
  19. }
  20. for (qreal y = top; y < rect.bottom(); y += gridSize) {
  21. lines.append(QLineF(rect.left(), y, rect.right(), y));
  22. }
  23. // 绘制网格
  24. painter->drawLines(lines.data(), lines.size());
  25. }