Fixed aspect ratio for buttons



  • 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?


  • Lifetime Qt Champion

    Hi,

    Isn't setFixedSize what you are looking for ?



  • 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...


  • Lifetime Qt Champion



  • 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...


  • Moderators

    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.



  • @ 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...


  • Moderators

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



  • 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...


  • Moderators

    [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 :)



  • 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.


  • Moderators

    [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!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.