Proportionnal resizing of window
-
Hi,
I would like my window to resize by keeping horizontal/vertical ratio the same.. Like when I grab the right edge of the window it also resize vertically and vice-versa.. I try overloading the resizeEvent method but at best I get horrible flickers due to the recursion.. What is the appropriate way to do this in a clean manner?
Thx
-
I'm assuming that the infinite recursion you're experiencing is because you call this->resize() inside of of a widget's resizeEvent. You should override the resizeEvent of the parent of the widget you want to resize. This is because a QResizeEvent is delivered to a widget in response to that same widget being resized, and is designed to allow a widget to respond to the size change by updating its children/members.
Alternatively, you could create your own layout manager class that sizes its children by percentage. You can possibly adapt the "Border Layout Example":http://qt-project.org/doc/qt-5/qtwidgets-layouts-borderlayout-example.html to get the effect you're looking for.
-
Hi,
Since there is already an option to resize the window through grabing the right edge of the window. then, why you want to do it again ??
In my point of view,
And if it is must to do this. then you just have to put conditions in resizeEvent .( If height is not equal to old height then change width according to height difference.
or
If width is not equal to old width then change height according to width difference.)
Such kind of logic you should put in resizeEvent.
I hope it help.
-
The Widget I want to keep ratio the same is the main window... not a child widget.. and yes I do call this->resize() in the resizeEvent... I don't get infinite recursion, but the event gets called for every pixel change which create very ugly flickers..
[quote author="adolby" date="1402361318"]I'm assuming that the infinite recursion you're experiencing is because you call this->resize() inside of of a widget's resizeEvent. You should override the resizeEvent of the parent of the widget you want to resize. This is because a QResizeEvent is delivered to a widget in response to that same widget being resized, and is designed to allow a widget to respond to the size change by updating its children/members.
Alternatively, you could create your own layout manager class that sizes its children by percentage. You can possibly adapt the "Border Layout Example":http://qt-project.org/doc/qt-5/qtwidgets-layouts-borderlayout-example.html to get the effect you're looking for.[/quote]
-
That's exactly what I did... read what I said again... and I said when I grab the right edge I want the window to resize vertically as well as horizontally ..
[quote author="ankursaxena" date="1402375559"]Hi,
Since there is already an option to resize the window through grabing the right edge of the window. then, why you want to do it again ??
In my point of view,
And if it is must to do this. then you just have to put conditions in resizeEvent .( If height is not equal to old height then change width according to height difference.
or
If width is not equal to old width then change height according to width difference.)
Such kind of logic you should put in resizeEvent.
I hope it help.
[/quote]
-
I'm sorry, I misread your requirements slightly. You want to maintain aspect ratio on your main window. I don't think that this is possible.
However, it's easy to achieve a similar effect. You can resize child widgets and keep their aspect ratio, as I suggested earlier: override resizeEvent() in your main window to update child widget size. You could also override the heightForWidth() method. Additionally, there is a layout manager class you could use called QSingleItemSquareLayout which can be found among Nokia's example code.
Here is a StackOverflow thread addressing your question:
http://stackoverflow.com/q/452333/1583123Here is another StackOverflow thread showing heightForWidth() method override:
http://stackoverflow.com/q/7259847/1583123You can find QSingleItemSquareLayout here: http://developer.nokia.com/community/wiki/Archived:Maintaining_square_form_for_a_QWidget