Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. improving the layout positions in a QGridLayout
Forum Updated to NodeBB v4.3 + New Features

improving the layout positions in a QGridLayout

Scheduled Pinned Locked Moved Unsolved Qt for Python
6 Posts 3 Posters 816 Views 2 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.
  • O Offline
    O Offline
    otaolafr
    wrote on last edited by
    #1

    Hello,
    I am having issues with the creation of an interface, the last column is disproportionated (due to a row that takes all the columns which is way larger than the rest)
    here is an image of what I am having right now and what I am looking to do:
    39618727-240f-4f8b-8907-5509c28fa5b2-image.png
    as it can be seen in the first image (actual output of my script) the column 2 is quiet large, this is due to the row 1 and 5 that are really large and they are setted to be in the 3 columns, what I would like to get (is shown in the second image) is that:

    1. the 'extra space of the row 1 and 5 is puted in the second column (column 1) and not in the third one.
    2. align the elements in the second column to the right (as in the first row), *notice that the element in the second column last row is a checkbox.
    dialog = QDialog()
    dialog.setWindowTitle("Input size")
    layout_0 = QGridLayout(dialog)
    #row 0
    checkBox=QCheckBox("text")
    checkBox.setChecked(1)
    label_ratio=QLabel("<b>text:</b>")
    
    inputsLines_1=QLineEdit(str(min(listOfMinSize)*minLengthFactorByDefault))
    inputsLines_1CurrentItem=inputsLines_1.text()
    layout_0.addWidget(checkBox,0,0)
    layout_0.addWidget(label_ratio,0,2)
    layout_0.addWidget(inputsLines_1,0,3)
    label_ratio.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
    
    #row 1
    sepBar_1=QLabel("<b>=====================================================================================================================================</b>")
    sepBar_1.setAlignment(QtCore.Qt.AlignCenter| QtCore.Qt.AlignVCenter)
    layout_0.addWidget(sepBar_1,1,0,1,4)
    
    #row 2
    scroll_1 = QScrollArea(dialog)             # scroll_1 Area which contains the widgets, set as the centralWidget
    layout_1 =QGridLayout()
    customWidget_1= QWidget()
    
    customWidget_1.setLayout(layout_1)
    scroll_1.setWidget(customWidget_1)
    scroll_1.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
    scroll_1.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
    layout_0.addWidget(scroll_1,2,0,1,4)
    
    lenScroll_1=6
    #row 3
    aclaration_1=QLabel("text")
    layout_0.addWidget(aclaration_1,3,0,1,4)
    aclaration_1.setWordWrap(True)
    #row 4
    aclaration_2=QLabel("text")
    layout_0.addWidget(aclaration_2,4,0,1,4)
    aclaration_2.setWordWrap(True)
    
    #row 5
    sepBar_3=QLabel("<b>=====================================================================================================================================</b>")
    sepBar_3.setAlignment(QtCore.Qt.AlignCenter| QtCore.Qt.AlignVCenter)
    layout_0.addWidget(sepBar_3,5,0,1,4)
    
    #row 6
    loadSTEPpatches=QPushButton("text")
    
    checkBox_dump=QCheckBox("Create \"smart\" dump File")
    checkBox_dump.setChecked(0)
    
    okbox = QDialogButtonBox(dialog)
    okbox.setOrientation(QtCore.Qt.Horizontal)
    okbox.setStandardButtons(QDialogButtonBox.Cancel|QDialogButtonBox.Ok)
    
    layout_0.addWidget(loadSTEPpatches,6,0)
    layout_0.addWidget(checkBox_dump,6,2)
    layout_0.addWidget(okbox,6,3)
    

    thanks in advance,

    JoeCFDJ 1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by A Former User
      #2

      Hi

      you might want to try

      • setting a fixed layout policy to your topright text edit (showing value 0.25), but this would imply that you have to set the width equal to the one of the button group ?
      • try another horizontal layout policy (be careful they are counter-intuitive)
      • use a horizontal spacer between it and the checkbox on topleft, with same problem mentionned above ?

      or maybe some combination of third with one of the two first

      O 1 Reply Last reply
      0
      • ? A Former User

        Hi

        you might want to try

        • setting a fixed layout policy to your topright text edit (showing value 0.25), but this would imply that you have to set the width equal to the one of the button group ?
        • try another horizontal layout policy (be careful they are counter-intuitive)
        • use a horizontal spacer between it and the checkbox on topleft, with same problem mentionned above ?

        or maybe some combination of third with one of the two first

        O Offline
        O Offline
        otaolafr
        wrote on last edited by
        #3

        @ankou29666 hello! thanks for your answer!
        for the first solution, I really did not wanted to fix the width as I dont know the length of the 0.25 so might be way larger, and therefore the fixed width would be a problem, as when I dont fix it the width will adapt to the value inside (obviously in this case it adapts to the second row so yeah it is too big....)
        for the second and third thing you mentionned I did not understood :/ could you elaborate or give an example, please?
        thanks

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          the horizontal spacer is the blue bar that visible in qt creator (first screenshot) that gets invisible in the running app (second screenshot).
          In qt creator you can see it in the list of available widgets (second column). Basically it takes as much space as possible to push other widgets in other directions (here it pushes the two comboboxes to the right). It is not visible on the screenshot but spacer and the two combobox are inside a row layout).

          The size policy defines how widgets behave when they are inserted inside layouts. Will they expand as much as they can ? will they take a fixed size ? will they take the minimum they need ?

          if you want to work with layouts, size policy and spacers are something you have to learn to work with.

          screenshot-qtcreator.jpg

          In this example, the spacer is expanding (logic) and both comboboxes have fixed policy, that is, the two comboboxes defin

          but as you design your ui without qt creator you will have to find by yourself how to use them by code. But i'm not certain this is the right solution, or at least not alone.

          screenshot-app.jpg

          On this last screenshot you see something you don't see on the first one because it is inserted at runtime, but the right container has a fixed policy (it takes just the amount of width needed), i think the contained QQuickWidget does also, while the left frame has preferred policy, it will take the rest.

          Documentation about size policy is here and this is something you should learn about in order to work with layouts.

          1 Reply Last reply
          1
          • O otaolafr

            Hello,
            I am having issues with the creation of an interface, the last column is disproportionated (due to a row that takes all the columns which is way larger than the rest)
            here is an image of what I am having right now and what I am looking to do:
            39618727-240f-4f8b-8907-5509c28fa5b2-image.png
            as it can be seen in the first image (actual output of my script) the column 2 is quiet large, this is due to the row 1 and 5 that are really large and they are setted to be in the 3 columns, what I would like to get (is shown in the second image) is that:

            1. the 'extra space of the row 1 and 5 is puted in the second column (column 1) and not in the third one.
            2. align the elements in the second column to the right (as in the first row), *notice that the element in the second column last row is a checkbox.
            dialog = QDialog()
            dialog.setWindowTitle("Input size")
            layout_0 = QGridLayout(dialog)
            #row 0
            checkBox=QCheckBox("text")
            checkBox.setChecked(1)
            label_ratio=QLabel("<b>text:</b>")
            
            inputsLines_1=QLineEdit(str(min(listOfMinSize)*minLengthFactorByDefault))
            inputsLines_1CurrentItem=inputsLines_1.text()
            layout_0.addWidget(checkBox,0,0)
            layout_0.addWidget(label_ratio,0,2)
            layout_0.addWidget(inputsLines_1,0,3)
            label_ratio.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
            
            #row 1
            sepBar_1=QLabel("<b>=====================================================================================================================================</b>")
            sepBar_1.setAlignment(QtCore.Qt.AlignCenter| QtCore.Qt.AlignVCenter)
            layout_0.addWidget(sepBar_1,1,0,1,4)
            
            #row 2
            scroll_1 = QScrollArea(dialog)             # scroll_1 Area which contains the widgets, set as the centralWidget
            layout_1 =QGridLayout()
            customWidget_1= QWidget()
            
            customWidget_1.setLayout(layout_1)
            scroll_1.setWidget(customWidget_1)
            scroll_1.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
            scroll_1.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
            layout_0.addWidget(scroll_1,2,0,1,4)
            
            lenScroll_1=6
            #row 3
            aclaration_1=QLabel("text")
            layout_0.addWidget(aclaration_1,3,0,1,4)
            aclaration_1.setWordWrap(True)
            #row 4
            aclaration_2=QLabel("text")
            layout_0.addWidget(aclaration_2,4,0,1,4)
            aclaration_2.setWordWrap(True)
            
            #row 5
            sepBar_3=QLabel("<b>=====================================================================================================================================</b>")
            sepBar_3.setAlignment(QtCore.Qt.AlignCenter| QtCore.Qt.AlignVCenter)
            layout_0.addWidget(sepBar_3,5,0,1,4)
            
            #row 6
            loadSTEPpatches=QPushButton("text")
            
            checkBox_dump=QCheckBox("Create \"smart\" dump File")
            checkBox_dump.setChecked(0)
            
            okbox = QDialogButtonBox(dialog)
            okbox.setOrientation(QtCore.Qt.Horizontal)
            okbox.setStandardButtons(QDialogButtonBox.Cancel|QDialogButtonBox.Ok)
            
            layout_0.addWidget(loadSTEPpatches,6,0)
            layout_0.addWidget(checkBox_dump,6,2)
            layout_0.addWidget(okbox,6,3)
            

            thanks in advance,

            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by
            #5

            @otaolafr
            choose stretch to 4 or 5 and try it out.
            layout_0->setColumnStretch(int column, int stretch)

            O 1 Reply Last reply
            1
            • JoeCFDJ JoeCFD

              @otaolafr
              choose stretch to 4 or 5 and try it out.
              layout_0->setColumnStretch(int column, int stretch)

              O Offline
              O Offline
              otaolafr
              wrote on last edited by
              #6

              @JoeCFD thanks! this solved my issue, setting the second column to 4 did the trick!
              for anyone that comes to this post the solution was

              layout_0.setColumnStretch(1,4)
              
              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