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. Fill all space in QLayout
Qt 6.11 is out! See what's new in the release blog

Fill all space in QLayout

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 6.3k Views 1 Watching
  • 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.
  • ivanicyI Offline
    ivanicyI Offline
    ivanicy
    wrote on last edited by
    #1

    Hello!!

    I have a custom widget that includes a QLabel and a QGraphicsView. I put this widgets in a layout by code but I don't know how to fill all the space available:

    0_1547637823265_e37730ab-40d0-4e21-8195-57bfbc0bf196-image.png

    I want this effect:

    0_1547638009053_f88cda39-637c-477b-87b9-340dfea58c05-image.png

    This is my code:

    void GraphicsWindow::displayGraphics(int iNumCols) {
    
        CUtil::clearLayout(ui->layoutCharts);
    
        int iRow = 0, iCol = 0;
        for (int iWidget = 0; iWidget < m_vectorWidgets.size(); iWidget++) {
            QWidget *widget = m_vectorWidgets.at(iWidget);
            widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            ui->layoutCharts->addWidget(widget, iRow, iCol, Qt::AlignCenter);
            iCol++;
    
            if (iCol == iNumCols) {       // iNumcols is the number of columns that I want
                iCol = 0;
                iRow++;
            }
        }
    }
    

    How can I fix it?

    Thank you very much!!

    J.HilkJ 1 Reply Last reply
    0
    • ivanicyI ivanicy

      Hello!!

      I have a custom widget that includes a QLabel and a QGraphicsView. I put this widgets in a layout by code but I don't know how to fill all the space available:

      0_1547637823265_e37730ab-40d0-4e21-8195-57bfbc0bf196-image.png

      I want this effect:

      0_1547638009053_f88cda39-637c-477b-87b9-340dfea58c05-image.png

      This is my code:

      void GraphicsWindow::displayGraphics(int iNumCols) {
      
          CUtil::clearLayout(ui->layoutCharts);
      
          int iRow = 0, iCol = 0;
          for (int iWidget = 0; iWidget < m_vectorWidgets.size(); iWidget++) {
              QWidget *widget = m_vectorWidgets.at(iWidget);
              widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
              ui->layoutCharts->addWidget(widget, iRow, iCol, Qt::AlignCenter);
              iCol++;
      
              if (iCol == iNumCols) {       // iNumcols is the number of columns that I want
                  iCol = 0;
                  iRow++;
              }
          }
      }
      

      How can I fix it?

      Thank you very much!!

      J.HilkJ Online
      J.HilkJ Online
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      hi @ivanicy
      said in Fill all space in QLayout:

      m_vectorWidgets

      I asume your custom widget, that contains the QLabel and QGraphicsView, is inside the m_vectorWidgets ?

      the code you showed seems good. So it can only be one of two things, that I can think of:

      • you have set a maxSize to the widget that is rather small the Layout will not resize its content beyond the maxsize of the items
      • QLabel and QGraphicsView are not inside a Layout themself and therefore do not get resized when the parent/container resizes

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      ivanicyI 1 Reply Last reply
      2
      • J.HilkJ J.Hilk

        hi @ivanicy
        said in Fill all space in QLayout:

        m_vectorWidgets

        I asume your custom widget, that contains the QLabel and QGraphicsView, is inside the m_vectorWidgets ?

        the code you showed seems good. So it can only be one of two things, that I can think of:

        • you have set a maxSize to the widget that is rather small the Layout will not resize its content beyond the maxsize of the items
        • QLabel and QGraphicsView are not inside a Layout themself and therefore do not get resized when the parent/container resizes
        ivanicyI Offline
        ivanicyI Offline
        ivanicy
        wrote on last edited by
        #3

        @J.Hilk

        I have put the widgets like the second image by default (when I run my program, they appear like the second image). To give functionality to the layout buttons, I put the widgets in this vector. But when I put again the widgets by code, they appear like the first image.

        I don't set any size to the widgets. I don't know what can I do.

        J.HilkJ 1 Reply Last reply
        0
        • ivanicyI ivanicy

          @J.Hilk

          I have put the widgets like the second image by default (when I run my program, they appear like the second image). To give functionality to the layout buttons, I put the widgets in this vector. But when I put again the widgets by code, they appear like the first image.

          I don't set any size to the widgets. I don't know what can I do.

          J.HilkJ Online
          J.HilkJ Online
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @ivanicy very particular,

          I just tested it in a small example, remove Qt::AlignCenter and it should work.

          setting the alignment to Qt::AlignCenter seems to result in a disregard of the SizePolicy of the layout widgets.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          ivanicyI 1 Reply Last reply
          4
          • J.HilkJ J.Hilk

            @ivanicy very particular,

            I just tested it in a small example, remove Qt::AlignCenter and it should work.

            setting the alignment to Qt::AlignCenter seems to result in a disregard of the SizePolicy of the layout widgets.

            ivanicyI Offline
            ivanicyI Offline
            ivanicy
            wrote on last edited by
            #5

            @J.Hilk
            Wow! thank you very much!! That works fine!

            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