qt - 拖放时将 QGraphics 项目与网格对齐

标签 qt grid qgraphicsview qt5 qgraphicsscene

例如,如果我想使用 QGraphicsView 在游戏中显示玩家的库存,我如何强制执行基于网格的 View ,其中拖放项目始终会导致它们与网格?

最佳答案

您可以在QGraphicsScene子类中处理适当的鼠标事件:

#include <QApplication>
#include <QGraphicsItem>
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsView>
#include <QMainWindow>

#include <math.h>

class GridScene : public QGraphicsScene
{
public:
    GridScene() :
        mCellSize(25, 25)
    {
    }
protected:
    // Efficiently draws a grid in the background.
    // For more information: http://www.qtcentre.org/threads/5609-Drawing-grids-efficiently-in-QGraphicsScene?p=28905#post28905
    void drawBackground(QPainter *painter, const QRectF &rect)
    {
        qreal left = int(rect.left()) - (int(rect.left()) % mCellSize.width());
        qreal top = int(rect.top()) - (int(rect.top()) % mCellSize.height());

        QVarLengthArray<QLineF, 100> lines;

        for (qreal x = left; x < rect.right(); x += mCellSize.width())
            lines.append(QLineF(x, rect.top(), x, rect.bottom()));
        for (qreal y = top; y < rect.bottom(); y += mCellSize.height())
            lines.append(QLineF(rect.left(), y, rect.right(), y));

        painter->drawLines(lines.data(), lines.size());
    }

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

    void 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);
    }

    void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
    {
        if (mDragged) {
            int x = floor(mouseEvent->scenePos().x() / mCellSize.width()) * mCellSize.width();
            int y = floor(mouseEvent->scenePos().y() / mCellSize.height()) * mCellSize.height();
            mDragged->setPos(x, y);
            mDragged = 0;
        } else
            QGraphicsScene::mouseReleaseEvent(mouseEvent);
    }
private:
    // The size of the cells in the grid.
    const QSize mCellSize;
    // The item being dragged.
    QGraphicsItem *mDragged;
    // The distance from the top left of the item to the mouse position.
    QPointF mDragOffset;
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QMainWindow w;
    w.resize(400, 400);
    w.show();

    QGraphicsView view(&w);
    view.setScene(new GridScene());
    view.resize(w.size());
    view.setSceneRect(0, 0, view.size().width(), view.size().height());
    view.show();

    view.scene()->addRect(0, 0, 25, 25)->setBrush(QBrush(Qt::blue));

    return a.exec();
}

#include "main.moc"

关于qt - 拖放时将 QGraphics 项目与网格对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15054117/

相关文章:

qt - 如何在 ListView 的项目之间设置自定义分隔符

javascript - 网格动画效果

c++ - 找到绘制的线的交点

python - 将 firefox 配置文件传递给远程 webdriver firefox 实例不起作用

python - 使用 PyQt 自定义 QDial notch ticks

python - QGraphicsGridLayout 适用于 PySide 但不适用于 PyQt4

c++ - 如何避免使用类型名称类型

windows - Qt + win32 + mingw 上的原生 Windows API 链接问题

c++ - 我应该如何在 Qt 中构建我的 GUI?

angular - 如何在网格列表中按 Material 使用 ngfor