QList<QGraphicsItem*> selectedList = selectedItems(); // check for grid snap if (!selectedList.isEmpty() && m_gridEnabled) { // correct selected items' coordinates for (QList<QGraphicsItem*>::iterator it = selectedList.begin(); it != selectedList.end(); it++) { QPointF itemPos = (*it)->scenePos(); (*it)->setPos(QPointF( (int)itemPos.x() - (int)itemPos.x() % m_gridSize, (int)itemPos.y() - (int)itemPos.y() % m_gridSize)); } return; }
inline qreal MyScene::round(qreal val, int step){ int tmp = int(val) + step ; tmp -= tmp % step; return qreal(tmp);}void MyScene::drawBackground(QPainter *painter, const QRectF &rect){ int step = 50; qreal start = round(rect.top(), step); if (start > rect.top()) { start -= step; } for (qreal y = start ; y < rect.bottom(); ) { y += step; painter->drawLine(rect.left(), y, rect.right(), y); } start = round(rect.left(), step); if (start > rect.left()) { start -= step; } for (qreal x = start - step; x < rect.right(); ) { x += step; painter->drawLine(x, rect.top(), x, rect.bottom()); }}
qreal left = int(rect.left()) - (int(rect.left()) % 50); qreal top = int(rect.top()) - (int(rect.top())% 50); QVarLengthArray<QLineF, 100> lines; for(qreal x= left; x < rect.right(); x += 50) lines.append(QLineF(x, rect.top(),x,rect.bottom())); for (qreal y = top; y < rect.bottom(); y += 50) lines.append(QLineF(rect.left(), y, rect.right(), y)); painter->drawLines(lines.data(),lines.size());