【问题标题】:geometry of QGraphicsRectItemQGraphicsRectItem 的几何形状
【发布时间】:2018-05-04 04:25:58
【问题描述】:

我在 qgraphicsScene 上绘制了一个 qgraphicsRectItem。使用鼠标事件,它在场景上移动、调整大小和重新定位,即选择项目、mousePress 和mouseMove。如何在 mouseReleaseEvent 上获取 qgraphicsRectItem boundingRect、pos wrt 场景的几何形状? 场景中有一个图像,并且在场景上绘制了一个 qgraphicsRectItem 的 boundingrect,然后我需要获取 qrect 以在边界矩形内裁剪该部分图像。

【问题讨论】:

    标签: c++ qt qt5 qgraphicsscene qgraphicsrectitem


    【解决方案1】:

    你必须使用mapRectToScene()的物品:

    it->mapRectToScene(it->boundingRect());
    

    例子:

    #include <QApplication>
    #include <QGraphicsRectItem>
    #include <QGraphicsSceneMouseEvent>
    #include <QGraphicsView>
    
    #include <QDebug>
    
    class GraphicsScene: public QGraphicsScene{
    public:
        using QGraphicsScene::QGraphicsScene;
    protected:
        void mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
            print_items();
            QGraphicsScene::mouseReleaseEvent(event);
        }
        void mouseMoveEvent(QGraphicsSceneMouseEvent *event){
             print_items();
            QGraphicsScene::mouseMoveEvent(event);
        }
    private:
        void print_items(){
            for(QGraphicsItem *it: items()){
                qDebug()<< it->data(Qt::UserRole+1).toString()<< it->mapRectToScene(it->boundingRect());
            }
        }
    };
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QGraphicsView w;
        GraphicsScene scene(0, 0, 400, 400);
        w.setScene(&scene);
    
        QGraphicsRectItem *item = new QGraphicsRectItem(QRectF(-10, -10, 20, 20));
        item->setData(Qt::UserRole+1, "item1");
        item->setBrush(QBrush(Qt::green));
        item->setFlags(QGraphicsItem::ItemIsMovable);
    
        QGraphicsRectItem *item2 = new QGraphicsRectItem(QRectF(0, 0, 20, 20));
        item2->setData(Qt::UserRole+1, "item2");
        item2->setBrush(QBrush(Qt::blue));
        item2->setFlags(QGraphicsItem::ItemIsMovable);
    
        scene.addItem(item);
        scene.addItem(item2);
        w.show();
    
        return a.exec();
    }
    

    【讨论】:

    • 是物品的意思吗?
    • @SayanBera “它”是 QGraphicsRectItem
    猜你喜欢
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多