QGraphicsRectItem - mapToParent and mapFromParent



  • I'm confused about that functions :s
    @
    #include <QApplication>
    #include <QGraphicsView>
    #include <QDebug>
    #include <QGraphicsRectItem>

    int main(int argc, char **argv)
    {
    QApplication app(argc, argv);
    QGraphicsScene scene;

    scene.setSceneRect(0,0,400,600);
    
    QGraphicsView view(&scene);
    
    scene.setBackgroundBrush(QBrush(Qt::black, Qt::SolidPattern));
    
    view.setFixedSize(400,600);
    view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view.setStyleSheet( "QGraphicsView { border-style: none; }" );
    
    QGraphicsRectItem *item1 = new QGraphicsRectItem(100,0,200,200);
    scene.addItem(item1);
    
    QGraphicsRectItem *item2 = new QGraphicsRectItem(0,0,100,100,item1);
    
    qDebug()<<item2->mapToParent(10,10);
    qDebug()<<item2->mapFromParent(10,10);
    
    item1->setBrush(QBrush(Qt::gray, Qt::SolidPattern));
    item2->setBrush(QBrush(Qt::red, Qt::SolidPattern));
    
    view.showFullScreen();
    
    return app.exec&#40;&#41;;
    

    }@

    The result is:
    QPointF(10, 10)
    QPointF(10, 10)

    For me the result should be:
    QPointF(110, 10)
    QPointF(-110, 10)

    :s



  • Yes, all your scene, item1, and item2 are in the same a coordinate system, so the value should be the same.

    Perhaps what you want is setPos() ?



  • All i want is that 0,0 of item2 is 100,100 of scene (0,0 of item1)



  • If you want a point has two different coordinate values, you will need two different coordinate system, isn't it? One of easiest ways to do this is making their origin point at different position, that is why setPos() exists.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.