【问题标题】:restrict movable area of qgraphicsitem限制qgraphicsitem的可移动区域
【发布时间】:2011-04-02 16:08:23
【问题描述】:

有没有办法限制设置setFlag(ItemIsMovable)QRect 之类的QGraphicsItem 可以移动的区域?

我是 pyqt 的新手,并试图找到一种用鼠标移动项目的方法,并将其限制为仅垂直/水平。

【问题讨论】:

    标签: python pyqt pyqt4 qgraphicsview qgraphicsitem


    【解决方案1】:

    如果你想保留一个有限的区域,你可以重新实现 ItemChanged()

    声明:

    #ifndef GRAPHIC_H
    #define GRAPHIC_H
    #include <QGraphicsRectItem>
    class Graphic : public QGraphicsRectItem
    {
    public:
        Graphic(const QRectF & rect, QGraphicsItem * parent = 0);
    protected:
        virtual QVariant    itemChange ( GraphicsItemChange change, const QVariant & value );
    };
    
    #endif // GRAPHIC_H
    

    实施: 需要 ItemSendsGeometryChanges 标志来捕捉 QGraphicsItem 的位置变化

    #include "graphic.h"
    #include <QGraphicsScene>
    
    Graphic::Graphic(const QRectF & rect, QGraphicsItem * parent )
        :QGraphicsRectItem(rect,parent)
    {
        setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemSendsGeometryChanges);
    }
    
    QVariant Graphic::itemChange ( GraphicsItemChange change, const QVariant & value )
    {
        if (change == ItemPositionChange && scene()) {
            // value is the new position.
            QPointF newPos = value.toPointF();
            QRectF rect = scene()->sceneRect();
            if (!rect.contains(newPos)) {
                // Keep the item inside the scene rect.
                newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left())));
                newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top())));
                return newPos;
            }
        }
        return QGraphicsItem::itemChange(change, value);
    }
    

    然后我们定义场景的矩形,在本例中为 300x300

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent)
    {
        QGraphicsView * view = new QGraphicsView(this);
        QGraphicsScene * scene = new QGraphicsScene(view);
        scene->setSceneRect(0,0,300,300);
        view->setScene(scene);
        setCentralWidget(view);
        resize(400,400);
    
        Graphic * graphic = new Graphic(QRectF(0,0,100,100));
        scene->addItem(graphic);
        graphic->setPos(150,150);
    
    }
    

    这是为了将图形保持在一个区域内, 祝你好运

    【讨论】:

    • 它工作得很好,期望图形项可以在右下方区域拖到场景之外一点,因为您正在使用图形框的左上角坐标来验证是否对象在场景矩形内。
    【解决方案2】:

    在 QGraphicScene 中重新实现 mouseMoveEvent(self,event) 像下面这样:

    def mousePressEvent(self, event ):
    
        self.lastPoint = event.pos()
    
    def mouseMoveEvent(self, point):
    
        if RestrictedHorizontaly: # boolean to trigger weather to restrict it horizontally 
            x = point.x()
            y = self.lastPoint.y()
            self.itemSelected.setPos(QtCore.QPointF(x,y))<br> # which is the QgraphicItem that you have or selected before
    

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      您可能需要重新实现QGraphicsItemitemChange() 函数。

      伪代码:

      if (object position does not meet criteria):
          (move the item so its position meets criteria)
      

      重新定位项目会导致再次调用itemChange,但这没关系,因为项目将正确定位并且不会再次移动,因此您不会陷入无限循环。

      【讨论】:

        猜你喜欢
        • 2014-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-27
        • 2014-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多