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. How to get the height of QVBoxLayout ?

How to get the height of QVBoxLayout ?

Scheduled Pinned Locked Moved Solved Qt for Python
11 Posts 5 Posters 6.6k 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.
  • N Offline
    N Offline
    NonNT
    wrote on last edited by
    #1

    How to get the height of QVBoxLayout ?

    titles = QVBoxLayout()
    titles.addLayout(QLabel('Title'))
    titles.addWidget(QLabel('SubTitle'))
    
    print(titles.contentsRect())  # =>  PySide2.QtCore.QRect(0, 0, 0, 0)
    

    Pyside2 5.13.1 on Manjaro

    jsulmJ JonBJ J.HilkJ 3 Replies Last reply
    0
    • N NonNT

      How to get the height of QVBoxLayout ?

      titles = QVBoxLayout()
      titles.addLayout(QLabel('Title'))
      titles.addWidget(QLabel('SubTitle'))
      
      print(titles.contentsRect())  # =>  PySide2.QtCore.QRect(0, 0, 0, 0)
      

      Pyside2 5.13.1 on Manjaro

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @NonNT Why do you need the height of a layout? Usually one is interested in size of a widget.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • N NonNT

        How to get the height of QVBoxLayout ?

        titles = QVBoxLayout()
        titles.addLayout(QLabel('Title'))
        titles.addWidget(QLabel('SubTitle'))
        
        print(titles.contentsRect())  # =>  PySide2.QtCore.QRect(0, 0, 0, 0)
        

        Pyside2 5.13.1 on Manjaro

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @NonNT
        Apart from what @jsulm has just said.

        I would assume the reason for QRect(0, 0, 0, 0) is that if the code is as you show the QVBoxLayout, and its widgets, are in limbo-land size-/layout-wise until you actually put them onto a window or actually show it? At minimum try putting the layout somewhere, and possibly have to wait till it's actually shown, not sure about that?

        1 Reply Last reply
        0
        • N NonNT

          How to get the height of QVBoxLayout ?

          titles = QVBoxLayout()
          titles.addLayout(QLabel('Title'))
          titles.addWidget(QLabel('SubTitle'))
          
          print(titles.contentsRect())  # =>  PySide2.QtCore.QRect(0, 0, 0, 0)
          

          Pyside2 5.13.1 on Manjaro

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

          @NonNT

          I don't know how the python bindings handle this, but this:

          titles.addLayout(QLabel('Title'))
          

          should actually not be possible

          besides that, @JonB is right, until shown, the content hight/width should be 0


          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.

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

            @NonNT

            I don't know how the python bindings handle this, but this:

            titles.addLayout(QLabel('Title'))
            

            should actually not be possible

            besides that, @JonB is right, until shown, the content hight/width should be 0

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @J-Hilk
            Good spot, I didn't actually notice that! You can add a QLayout to a QLayout, but you can't pass a QWidget to addlayout()!

            I don't know how the python bindings handle this

            I tried in PyQt5, and get run-time error:

            TypeError: addLayout(self, QLayout, stretch: int = 0): argument 1 has unexpected type 'QLabel'

            PySide2 run-time error:

            TypeError: 'PySide2.QtWidgets.QBoxLayout.addLayout' called with wrong argument types:
            PySide2.QtWidgets.QBoxLayout.addLayout(QLabel)
            Supported signatures:
            PySide2.QtWidgets.QBoxLayout.addLayout(PySide2.QtWidgets.QLayout, int = 0)

            Don't you just love it when Python has to run-time error where a decent language would pick it up at compile time :(

            J.HilkJ 1 Reply Last reply
            2
            • JonBJ JonB

              @J-Hilk
              Good spot, I didn't actually notice that! You can add a QLayout to a QLayout, but you can't pass a QWidget to addlayout()!

              I don't know how the python bindings handle this

              I tried in PyQt5, and get run-time error:

              TypeError: addLayout(self, QLayout, stretch: int = 0): argument 1 has unexpected type 'QLabel'

              PySide2 run-time error:

              TypeError: 'PySide2.QtWidgets.QBoxLayout.addLayout' called with wrong argument types:
              PySide2.QtWidgets.QBoxLayout.addLayout(QLabel)
              Supported signatures:
              PySide2.QtWidgets.QBoxLayout.addLayout(PySide2.QtWidgets.QLayout, int = 0)

              Don't you just love it when Python has to run-time error where a decent language would pick it up at compile time :(

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

              @JonB lovely
              do also not get a hint where in your files this happened?


              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.

              JonBJ 1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                @JonB lovely
                do also not get a hint where in your files this happened?

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @J-Hilk
                Yes, they both gave me a stacktrace/code lines. Didn't paste, but I didn't mean to imply they did not produce those. Just that you only discover when/if you hit the code at run-time, where code above would not compile in C++. Since I have to use Python for my Qt work, I like to moan about it a lot ;)

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  NonNT
                  wrote on last edited by
                  #8

                  @J-Hilk Sorry, yes, it should be addWidget

                  logo = QSvgWidget()
                  logo.load('logo.svg')
                  
                  titles = QVBoxLayout()
                  titles.addWidget(QLabel('Title'))
                  titles.addWidget(QLabel('SubTitle'))
                  
                  titleBox = QHBoxLayout()
                  titleBox.addWidget(logo)
                  titleBox.addLayout(titles)
                  

                  logo should have the same width as height, depending on the height of titles
                  but now the width of logo is dynamic, but should be fixed.
                  for logo.setFixedSize() I need the height of titles

                  thanks

                  J.HilkJ JonBJ 2 Replies Last reply
                  0
                  • N NonNT

                    @J-Hilk Sorry, yes, it should be addWidget

                    logo = QSvgWidget()
                    logo.load('logo.svg')
                    
                    titles = QVBoxLayout()
                    titles.addWidget(QLabel('Title'))
                    titles.addWidget(QLabel('SubTitle'))
                    
                    titleBox = QHBoxLayout()
                    titleBox.addWidget(logo)
                    titleBox.addLayout(titles)
                    

                    logo should have the same width as height, depending on the height of titles
                    but now the width of logo is dynamic, but should be fixed.
                    for logo.setFixedSize() I need the height of titles

                    thanks

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

                    @NonNT
                    for such cases, QWidgets have a int QWidget::heightForWidth(int w) const but you have to override that function for it to work as indented. I'm not sure how to do this with python ..


                    Here's an example on how to do such a thing in c++, still working in Qt5.12 +
                    https://stackoverflow.com/a/18923122


                    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.

                    1 Reply Last reply
                    2
                    • N NonNT

                      @J-Hilk Sorry, yes, it should be addWidget

                      logo = QSvgWidget()
                      logo.load('logo.svg')
                      
                      titles = QVBoxLayout()
                      titles.addWidget(QLabel('Title'))
                      titles.addWidget(QLabel('SubTitle'))
                      
                      titleBox = QHBoxLayout()
                      titleBox.addWidget(logo)
                      titleBox.addLayout(titles)
                      

                      logo should have the same width as height, depending on the height of titles
                      but now the width of logo is dynamic, but should be fixed.
                      for logo.setFixedSize() I need the height of titles

                      thanks

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #10

                      @NonNT said in How to get the height of QVBoxLayout ?:

                      for logo.setFixedSize() I need the height of titles

                      As @J-Hilk has said, QWidget::heightForWidth() looks like the best way to go. (I don't know if https://doc.qt.io/qt-5/qsizepolicy.html#setHeightForWidth does it for you without override.) But if you are still asking to get the size of the layout, I don't see you have acted on what we said earlier?

                      1 Reply Last reply
                      0
                      • N Offline
                        N Offline
                        NonNT
                        wrote on last edited by
                        #11

                        Don't know how to override QWidget::heightForWidth(int w)

                        But I can sum up the heights of the widgets and the spacing between the widgets.

                        title = QLabel(f'<h3>{self.title}</h3>')
                        subTitle = QLabel(self.subTitle)
                        
                        widgetTmp = QWidget()
                        vboxlayoutTmp = QVBoxLayout(widgetTmp)
                        vboxlayoutHeight = title.sizeHint().height() + vboxlayoutTmp.layout().spacing() + subTitle.sizeHint().height()
                        
                        logo = QSvgWidget()
                        logo.load('logo.svg')
                        logo.setFixedSize(vboxlayoutHeight, vboxlayoutHeight)
                        
                        labels = QVBoxLayout()
                        labels.addWidget(title)
                        labels.addWidget(subTitle)
                        
                        titleBox = QHBoxLayout()
                        titleBox.addWidget(logo)
                        titleBox.addLayout(labels)
                        
                        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