限制qgraphicsitem的可移动区域 [英] restrict movable area of qgraphicsitem

查看:118
本文介绍了限制qgraphicsitem的可移动区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

setFlag(ItemIsMovable) 被设置时,有没有办法限制QGraphicsItemQRect 可以移动的区域?>

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

解决方案

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

声明:

#ifndef GRAPHIC_H#define GRAPHIC_H#include 类图形:公共 QGraphicsRectItem{上市:图形(const QRectF & rect, QGraphicsItem * parent = 0);受保护:虚拟 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 变化, const QVariant & value ){如果(更改 == ItemPositionChange && 场景()){//value 是新位置.QPointF newPos = value.toPointF();QRectF rect = scene()->sceneRect();如果(!rect.contains(newPos)){//将项目保留在场景矩形内.newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left())));newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top())));返回新位置;}}返回 QGraphicsItem::itemChange(change, value);}

然后我们定义场景的矩形,在这种情况下将是 300x300

MainWindow::MainWindow(QWidget *parent) :QMainWindow(父){QGraphicsView * view = new QGraphicsView(this);QGraphicsScene * 场景 = 新 QGraphicsScene(view);场景-> setSceneRect(0,0,300,300);视图-> setScene(场景);setCentralWidget(视图);调整大小(400,400);图形 * 图形 = 新图形(QRectF(0,0,100,100));场景->添加项目(图形);图形-> setPos(150,150);}

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

Is there a way to restrict the area where a QGraphicsItem like QRect can be moved when setFlag(ItemIsMovable) is set?

I'm new to pyqt and trying to find a way to move an item with the mouse, and the restrict it to only vertically/horizontally.

解决方案

If you want to keep a limited area you can reimplement the ItemChanged()

Declare:

#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

implementation : ItemSendsGeometryChanges flag is needed to capture the change in position of 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);
}

Then we define the rectangle of the scene, in this case will be 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);

}

This is to keep the graph within an area, good luck

这篇关于限制qgraphicsitem的可移动区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆