Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Size QGraphicsView automatically
Forum Update on Monday, May 27th 2025

Size QGraphicsView automatically

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 3.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AlvaroSA Offline
    AlvaroSA Offline
    AlvaroS
    wrote on last edited by
    #1

    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
    AlvaroSA 1 Reply Last reply
    0
    • AlvaroSA AlvaroS

      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
      AlvaroSA Offline
      AlvaroSA Offline
      AlvaroS
      wrote on last edited by
      #2

      @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.

      Joel BodenmannJ 1 Reply Last reply
      0
      • AlvaroSA AlvaroS

        @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.

        Joel BodenmannJ Offline
        Joel BodenmannJ Offline
        Joel Bodenmann
        wrote on last edited by Joel Bodenmann
        #3

        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

        Industrial process automation software: https://simulton.com
        Embedded Graphics & GUI library: https://ugfx.io

        AlvaroSA 1 Reply Last reply
        1
        • Joel BodenmannJ Joel Bodenmann

          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

          AlvaroSA Offline
          AlvaroSA Offline
          AlvaroS
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved