QGraphicsLineItem line is not at the center of the item



  • I am drawing a QGraphicsLineItem line at the middle of the scene in QGraphicsView .But the lines doesn't appear at the (0,0) It has an offset of 5 pixels from the center on X axis.How do I align the line to be at it's bounds center?

    Here is my code:

    @

    QGraphicsLineItem *scrubber = new QGraphicsLineItem(0,0,0,100);

    scrubber->setPen(QPen(QColor(255,0,0,255)));
    gview->scene()->addItem(scrubber);

    scrubber->setPos(0.0f,0);

    @



  • Possible reasons:
    a) x=0 is not in the center of the graphics scene's sceneRect. You may e.g. have an additional horizontal line from x=-5 to x=15 units.
    b) the graphics scene is not displayed in the center of the graphics view.
    c) the graphics view is displayed with a margin.



  • Thanks.But how do I check these things?



  • First, check the QGraphicsView's alignment property.
    Then, read the sceneRect of the QGraphicsScene. If you only added the one line, it should be exactly big enough to hold that line.


Log in to reply
 

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