Error : No match for the operand

444 Views Asked by At

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?

1

There are 1 best solutions below

4
On

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.