Size QGraphicsView automatically



  • Hi users!!
    First of all thanks for helping me.

    I have a Scene in a GraphicView. In this Scene I depict lines which show a map.
    The problem is that the lines has not always the same size so I depict the lines sometimes small, sometimes big...
    I need to fit the map to the QGraphicsView window.

    Here my code is:

    QGraphicsScene *scene;
    QGraphicsLineItem *line;
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
       scene = new QGraphicsScene(this);
        ui->graphicsView->setScene(scene); //We associate the ui GraphicsView to Scene that we have created before.
    
        //connect(this,SIGNAL(print_lines_signal(QFile *)),this,SLOT(print_lines_slot(QFile *)));
    }
    
    ..........
    
    
            if(file.open(QFile::ReadOnly))
            {
    
                /*************************
                 pinta las líneas
                *************************/
                    QString text = file.readAll(); //Conversion from QFile to QString to be able to split the whole text in lines and words.
                    QStringList lines = text.split("\n", QString::SkipEmptyParts); //Split the whole text in lines and save them in an List called "lines"
                    QStringList words; //Words will save all the words in each lines. We will always have   words (x_start, y_start, x_end, y_end, psi, r)
    
                    int num_lines = lines.size(); //We obtein the number of lines that text has.
    
                    struct struc_lines lines_struct[num_lines]; //We create the line structury which will be an array structure with num_lines elements.
    
                    for (int i=0;i<num_lines;i++)
                    {
                        words = lines.at(i).split(" ", QString::SkipEmptyParts); // Split each line in words
                        lines_struct[i].x_start= words.at(0).toDouble(); //We take the first word and convert it to Double because it was QString type before
                        lines_struct[i].y_start= words.at(1).toDouble(); //Second word
                        lines_struct[i].x_end= words.at(2).toDouble(); //Third word
                        lines_struct[i].y_end= words.at(3).toDouble(); //Fourth word
    
                        QPen blue(Qt::blue);
                        blue.setWidth(1);
                        line = scene->addLine(lines_struct[i].x_start,lines_struct[i].y_start,lines_struct[i].x_end,lines_struct[i].y_end, blue); //Esta multiplicado por diez para que se vea mejor
    
                        std::cout<<i << " " << lines_struct[i].x_start<< std::endl;
                    }
        }
     
    Can anybody help me'
    Thanks


  • @AlvaroS I have done this:

    ui->graphicsView->scale(ui->graphicsView->width()/scene->width(),ui->graphicsView->height()/scene->height());
    

    the problem is that the scene fits well but not at all. My graphivsView always show a little scrollbars to see the part of the scene that is not showed in the graphicsview...

    How can I fix that¿

    Thanks.



  • You might want to have a look at QGraphicsView::setFixedSize(). Also, it's possible to disable the scroll bars using QGraphicsView::setXxxScrollBarPolicy()

    view->setFixedSize(view->scene()->width(), view->scene()->height());
    view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    

    Depending on your needs, you might want to combine that together with QGraphicsView::setFixedSize().
    Other functions that might help to archive what you want include QGraphicsView::ensureVisible() and QGraphicsView::fitInView().

    I hope that helps



  • @Joel-Bodenmann Thanks a lot. You helped me!!!


Log in to reply
 

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