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. Square QWidget - Height = Width
Forum Updated to NodeBB v4.3 + New Features

Square QWidget - Height = Width

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 3.7k 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.
  • webzoidW Offline
    webzoidW Offline
    webzoid
    wrote on last edited by
    #1

    Is there a nice way to ensure that, when resized, a QWidget remains square (height = width)?

    I have a couple of QWidgets in a layout and when the layout size changes, I want to enforce this fixed size scheme.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      You can use
      http://doc.qt.io/qt-5/qwidget.html#hasHeightForWidth
      and
      int QWidget::heightForWidth(int w) const
      Those are virtuals so you need to subclass to use.

      1 Reply Last reply
      0
      • webzoidW Offline
        webzoidW Offline
        webzoid
        wrote on last edited by
        #3

        @mrjj Thanks for your comment.

        I've just implemented both the hasHeightForWidth (returning true) and the heightForWidth function (returning the width) - which I'm guessing is the correct way to do it.

        The outcome is ultimately the same - the widget is not square.

        mrjjM 1 Reply Last reply
        0
        • webzoidW webzoid

          @mrjj Thanks for your comment.

          I've just implemented both the hasHeightForWidth (returning true) and the heightForWidth function (returning the width) - which I'm guessing is the correct way to do it.

          The outcome is ultimately the same - the widget is not square.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @webzoid
          Did you debug it ?
          Does it call your override ?

          1 Reply Last reply
          0
          • webzoidW Offline
            webzoidW Offline
            webzoid
            wrote on last edited by
            #5

            It does indeed, it calls both - many times

            mrjjM 1 Reply Last reply
            0
            • webzoidW webzoid

              It does indeed, it calls both - many times

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @webzoid

              HI you might also need sizeHint
              see this old sample
              https://doc.qt.io/archives/qq/qq04-height-for-width.html
              (the QFrame)
              Sorry could not find newer.

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                I fought this fight in the past.
                heightForWidth determines the preferred height for width not the actual one. any layout is free to go beyond it.

                if you can live with setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed); then use it, otherwise you'll need to create a custom layout

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                mrjjM 1 Reply Last reply
                1
                • VRoninV VRonin

                  I fought this fight in the past.
                  heightForWidth determines the preferred height for width not the actual one. any layout is free to go beyond it.

                  if you can live with setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed); then use it, otherwise you'll need to create a custom layout

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @VRonin
                  oh so it can't work with layouts at all ?

                  Thank you for enlightening us. I was pretty sure
                  it would work with layouts also.

                  1 Reply Last reply
                  0
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by VRonin
                    #9

                    I must admit last time I looked at it I was still a n00b of Qt (even though little changed in the meantime). I vaguely remember having a layout with a Qt::Alignment private member. in the setGeometry I calculated the minimum of width and height, resized the widget according to that minimum and then placed it according to the alignment set. It supported only 1 item but it wasn't a big issue as you can combine layouts.

                    If you are interested the code can be found here: header/source but, as I mentioned, it was a project I used to learn Qt so the code quality is surely below any reasonable standard

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    1

                    • Login

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