-1

I trying to implement snapping but getting an error

error: no match for 'operator%' (operand types are 'const int' and 'const QSize')
     const int firstLeftGridLine = realLeft - (realLeft % gridSize);

My code to it as follows: cadgraphicsscene.cpp

    void CadGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect)
    {
        const int realLeft = static_cast<int>(std::floor(rect.left()));
        const int realRight = static_cast<int>(std::ceil(rect.right()));
        const int realTop = static_cast<int>(std::floor(rect.top()));
        const int realBottom = static_cast<int>(std::ceil(rect.bottom()));

        // Draw grid.
        const int firstLeftGridLine = realLeft - (realLeft % gridSize);
        const int firstTopGridLine = realTop -(realTop % gridSize);
        QVarLengthArray<QLine, 100> lines;

        for (qreal x = firstLeftGridLine; x <= realRight; x += gridSize)
            lines.append(QLine(x, realTop, x, realBottom));
        for (qreal y = firstTopGridLine; y <= realBottom; y += gridSize)
            lines.append(QLine(realLeft, y, realRight, y));

        painter->setPen(QPen(QColor(220, 220, 220), 0.0));
        painter->drawLines(lines.data(), lines.size());

        // Draw axes.
        painter->setPen(QPen(Qt::lightGray, 0.0));
        painter->drawLine(0, realTop, 0, realBottom);
        painter->drawLine(realLeft, 0, realRight, 0);
    }

void CadGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    mDragged = qgraphicsitem_cast<QGraphicsItem*>(itemAt(mouseEvent->scenePos(),
QTransform()));
    if (mDragged) {
        mDragOffset = mouseEvent->scenePos() - mDragged->pos();
    } else
        QGraphicsScene::mousePressEvent(mouseEvent);
}

void CadGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
   if (mDragged) {
        int x = floor(mouseEvent->scenePos().x() / gridSize.width()) * gridSize.width();
        int y = floor(mouseEvent->scenePos().y() / gridSize.height()) * gridSize.height();
        mDragged->setPos(x, y);
        mDragged = 0;
    } else
        QGraphicsScene::mouseReleaseEvent(mouseEvent);
}

void CadGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    if (mDragged) {
        // Ensure that the item's offset from the mouse cursor stays the same.
        mDragged->setPos(mouseEvent->scenePos() - mDragOffset);
    } else
        QGraphicsScene::mouseMoveEvent(mouseEvent);
}

cadgraphicsscene.h

private:
    const QSize gridSize;
    QGraphicsItem *mDragged;
    QPointF mDragOffset;

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);
    void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);

I am getting error in cadgraphicsscene.cpp file.Can you please help me to solve the error?

user3859872
  • 777
  • 2
  • 9
  • 16
  • 2
    How can you divide int to QSize()? Even compiler cannot. You need to bring both arguments to the type `int`. For example: `realLeft % gridSize.width()` – vahancho Nov 25 '14 at 14:14

1 Answers1

2

QSize is a class that describes a two-dimensional size (width and height). I suspect you meant to use

realLeft % gridSize.width()

and

realTop  % gridSize.height()

respectively.

seldon
  • 510
  • 2
  • 4
  • When i use as stated above my system crashes. Unexpected thing but happens :(. Even I changed QSize datatype to int even then the it crashes. Please help me out to solve this – user3859872 Nov 25 '14 at 14:25
  • Make sure that neither gridSize.width() nor gridSize.height() are zero; that's about the only way this could cause a crash. – seldon Nov 25 '14 at 16:08
  • Even after assigning the value it crashes. I have added gridSize.width ==10; gridSize.width == 10; – user3859872 Nov 25 '14 at 16:21
  • Then you have a different error somewhere else, possibly in completely different code. Use your debugger. Reliably reproducible crashes are usually easy to find with it. – seldon Nov 25 '14 at 17:03