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. Fixed aspect ratio for buttons
Forum Updated to NodeBB v4.3 + New Features

Fixed aspect ratio for buttons

Scheduled Pinned Locked Moved General and Desktop
12 Posts 3 Posters 8.9k 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.
  • R Offline
    R Offline
    RuhigBrauner
    wrote on 3 Jul 2014, 12:37 last edited by
    #1

    Hi,

    It's a simple task but Qt just doesn't let me. I want to have square Buttons which expand to the biggest possible square. So if the given height is limiting, the buttons width and height get both set to the height (and vice versa).

    I tried using setFixedHeight(), which only works if the limit is always the width.
    Using both depending on the limiting factor doesn't work because no reziveEvents are called after that.
    Using something like resize(min,min) doesn't work because the layout doesn't know the "true" size of the Button and thus the button can't be centered anymore. (So resize(min,min) works but you can't use setLayout or something anymore)

    I don't know how overwriting heightForWidth(int w); should work but I would rather use something else since I read this method isn't supported for X11.

    So, why is it so hard to set a Widget to a fixed aspect ratio in Qt?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 3 Jul 2014, 22:56 last edited by
      #2

      Hi,

      Isn't setFixedSize what you are looking for ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • R Offline
        R Offline
        RuhigBrauner
        wrote on 4 Jul 2014, 10:42 last edited by
        #3

        Hi,
        I've noticed that an QWidget which size was set with setFixedSize wont receive any resizeEvents anymore. So auto-adjusting the size won't work.

        It worked with only using a fixed width or height. I also tried to reset the fixed size and then set one axis to be fixed again, somehow didn't worked...

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 4 Jul 2014, 13:13 last edited by
          #4

          "This":http://stackoverflow.com/questions/452333/how-to-maintain-widgets-aspect-ratio-in-qt may help you

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • R Offline
            R Offline
            RuhigBrauner
            wrote on 4 Jul 2014, 14:39 last edited by
            #5

            I've tried this already but somehow had no success. I'm a bit stunned that Qt doesn't provide an easy way to fix an aspect ratio...

            1 Reply Last reply
            0
            • J Online
              J Online
              JKSH
              Moderators
              wrote on 4 Jul 2014, 15:59 last edited by
              #6

              Hi,

              [quote author="RuhigBrauner" date="1404484755"]I'm a bit stunned that Qt doesn't provide an easy way to fix an aspect ratio...[/quote]I think reimplementing QWidget::resizeEvent() is pretty easy.

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              0
              • R Offline
                R Offline
                RuhigBrauner
                wrote on 5 Jul 2014, 09:41 last edited by
                #7

                @ JKSH,

                sure it is but what would you put in the function? Like I said, using setFixedSize() doesn't help because the widget doesn't receive any more resizeEvents after this. (However only fixing one side work.)

                Switching between the fixed axis (either limiting height to the width or vice versa) should work but I don't know hot to "un-fix" the other value...

                1 Reply Last reply
                0
                • J Online
                  J Online
                  JKSH
                  Moderators
                  wrote on 5 Jul 2014, 12:03 last edited by
                  #8

                  When you get a QResizeEvent, check the dimensions. If it's not square, call this->resize(min, min) where min is the shorter side.

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    RuhigBrauner
                    wrote on 5 Jul 2014, 19:17 last edited by
                    #9

                    With this Methode, i had the problem, that the Layout can't center the Widget. (Alignment has no effect.) i think, that this is behause the Layout doesnt know the "true" Site of the. Widget...

                    Sorry for the spelling, Auto correct on geman OS...

                    1 Reply Last reply
                    0
                    • J Online
                      J Online
                      JKSH
                      Moderators
                      wrote on 14 Jul 2014, 08:33 last edited by
                      #10

                      [quote author="RuhigBrauner" date="1404587823"]With this Methode, i had the problem, that the Layout can't center the Widget. (Alignment has no effect.) i think, that this is behause the Layout doesnt know the "true" Site of the. Widget...[/quote]Yes, you're right... it's because the layout calculated the position using the wrong size.

                      You can try moving the widget yourself too. Use setGeometry() instead of resize() -- this also lets you specify the position.

                      [quote]Sorry for the spelling, Auto correct on geman OS...[/quote]Don't worry, your words are clear :)

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        RuhigBrauner
                        wrote on 14 Jul 2014, 09:11 last edited by
                        #11

                        in which "space" does setGeometry work? In the parent-widget?

                        I think using a special layout-class which handles the proportions would be the best way.
                        Subclassing QHBoxLayout and implementing this feature shouldn't be so hard. (I just thought that a widget can handle this on itself without loosing functionalities of the layout...)

                        What I might do as a workaround is only changing the appearence of the buttons. I have a subclass for buttons which draw their own icons (based on a function pointer, they are given.) I could just adjust the bounding rectangle of the draw function to be square. This would have the problem that the clickable area is bigger than the icon but it should be optimized for mobile anyway,...

                        I will try setGeometry() as soon as I am working on the project again.

                        1 Reply Last reply
                        0
                        • J Online
                          J Online
                          JKSH
                          Moderators
                          wrote on 14 Jul 2014, 10:38 last edited by
                          #12

                          [quote author="RuhigBrauner" date="1405329083"]in which "space" does setGeometry work? In the parent-widget?[/quote]Correct.

                          If the widget doesn't have a parent, then the "space" is the whole screen.

                          [quote]I think using a special layout-class which handles the proportions would be the best way.[/quote]Yes, that's the more reliable method -- it will work on all existing widgets too.

                          See http://qt-project.org/doc/qt-5/layout.html#how-to-write-a-custom-layout-manager for details. Good luck!

                          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                          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