Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Size QGraphicsView automatically

    General and Desktop
    2
    4
    2996
    Loading More Posts
    • 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.
    • AlvaroS
      AlvaroS last edited by

      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 1 Reply Last reply Reply Quote 0
      • AlvaroS
        AlvaroS @AlvaroS last edited by

        @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 Bodenmann 1 Reply Last reply Reply Quote 0
        • Joel Bodenmann
          Joel Bodenmann @AlvaroS last edited by 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

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

          AlvaroS 1 Reply Last reply Reply Quote 1
          • AlvaroS
            AlvaroS @Joel Bodenmann last edited by

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

            1 Reply Last reply Reply Quote 0
            • First post
              Last post