使用qt捕捉网格

时间:2014-11-16 16:28:42

标签: qt qgraphicsview qgraphicsscene qvariant

我使用drawBackgroud方法在graphicsView中实现了网格。现在我还想在网格中添加snap。通过快照我的意思是,使用鼠标,除了网格点之外你不能有点。我绘制的网格代码如下:

void CadGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect)
{
    const int gridSize = 50;
    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);
}

请帮我解决问题并完成任务。


我尝试使用itemChange方法,但没有发生任何事情:

我的代码如下: snap.cpp

 #include "snap.h"
#include <QApplication>

    Snap::Snap(const QRect& rect, QGraphicsItem* parent,
               QGraphicsScene* scene):
    QGraphicsRectItem(QRectF())
    {
        setFlags(QGraphicsItem::ItemIsSelectable |
                QGraphicsItem::ItemIsMovable |
                QGraphicsItem::ItemSendsGeometryChanges);
    }

    void Snap::mousePressEvent(QGraphicsSceneMouseEvent *event){
        offset = pos() - computeTopLeftGridPoint(pos());
        QGraphicsRectItem::mousePressEvent(event);
    }

    QVariant Snap::itemChange(GraphicsItemChange change,
    const QVariant &value)
    {
        if (change == ItemPositionChange && scene()) {
            QPointF newPos = value.toPointF();
            if(QApplication::mouseButtons() == Qt::LeftButton &&
                qobject_cast<CadGraphicsScene*> (scene())){
                    QPointF closestPoint = computeTopLeftGridPoint(newPos);
                    return closestPoint+=offset;
                }
            else
                return newPos;
        }
        else
            return QGraphicsItem::itemChange(change, value);
    }

    QPointF Snap::computeTopLeftGridPoint(const QPointF& pointP){
       CadGraphicsScene* customScene = qobject_cast<CadGraphicsScene*> (scene());
        int gridSize = customScene->getGridSize();
        qreal xV = floor(pointP.x()/gridSize)*gridSize;
        qreal yV = floor(pointP.y()/gridSize)*gridSize;
        return QPointF(xV, yV);
    }

snap.h

#ifndef SNAP_H
#define SNAP_H

#include <QGraphicsRectItem>
#include "cadgraphicsscene.h"

class Snap : public QGraphicsRectItem
{
public:
    Snap(const QRect& rect, QGraphicsItem* parent,
         QGraphicsScene* scene);
protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    QVariant itemChange(GraphicsItemChange change,
    const QVariant &value);
private:
    QPointF offset;
    QPointF computeTopLeftGridPoint(const QPointF &pointP);
};

#endif // SNAP_H

请帮我解决问题。

0 个答案:

没有答案