Important: Please read the Qt Code of Conduct -

drawing line with QGraphicsView and QGraphicsscene

  • QGraphicsView, QGraphicsscene,addLine

    Hi, I am new to Qt and I wanted to draw some grid lines in the scene in the constructor as a guide for the user to click and draw shapes.

      c1 = new QGraphicsScene(0,0,400,400,this); 
        cv1= new QGraphicsView(c1,this);

    Given that both my scene and view are bound by rect 400,400, I want to draw say 10 lines as guide/grid on the scene. It seems if I take c1->height() and c1->wdith(), I can addLines in the scene. But, when I change the size of my application window, the lines do not readjust in cv1. Any easy way to accomplish this?

    Here is the drawGrid routine that is called from the constructor:

    void drawGrid() {
        for (int i=1; width < (400);i++){
            width= (400)/10 * i;
          lineItemPtr = c1->addLine(width,0,width,400 );

  • @newQter hi,friend,welcome.

    do you want to readjust line's position value when the window reset the size?

    if yes. you should reimplement QWidget::resizeEvent function, and to adjust scene size and line's position value.

    you should know when window size changed, the scene in QGraphicsView not to change it's size. and the line's position value also not to change.

Log in to reply