[Qt-interest] Aspect Ratio with QWidget::heightForWidth(int)

Huseyin Kozan huseyinkozan at gmail.com
Wed Jul 22 19:39:19 CEST 2009


I found a nice layout example and changed it for fixed ratio rectangle which
start at top left  from here:
http://wiki.forum.nokia.com/index.php/CS001309_-_Maintaining_square_form_for_a_widget_in_Qt

I attached the whole project. I compiled it with 4.5.2, mingw, creator
1.2.1. Maybe someone needs again.


Hüseyin

On Wed, Jul 22, 2009 at 18:47, Huseyin Kozan <huseyinkozan at gmail.com> wrote:

> Thank you for the reply David.
> I did another way before this try like this:
>
> void MainWindow::resizeEvent(QResizeEvent * event)
> {
>         resizeSessionWidget();
> }
>
> /** Resize sessionWidget to keep aspect ratio. */
> void MainWindow::resizeSessionWidget(void)
> {
>     double ratio = 1.4;
>     int w = sessionWidget->width();
>     int h = sessionWidget->height();
>     int calc_h = h ;
>     int calc_w = h * ratio;
>     if (calc_w > w ) {
>         calc_w = w;
>         calc_h = w / ratio;
>     }
>     sessionWidget->resize(calc_w, calc_h);
> }
>
> /** Resizing process with delay. */
> void MainWindow::resizeSWWithDelay(void)
> {
>     QTimer::singleShot(0, this, SLOT(resizeSessionWidget()));
> }
>
> I have some trouble with this design. When resizing, no problem. but when I
> show or hide some ather widgets I need to resize it with delay. It is so
> irritating to think about all possible resize needs at the run time.
>
> I remember that there is another way do it with the layouts. That mentioned
> in here :
>
> http://stackoverflow.com/questions/452333/how-to-maintain-widgets-aspect-ratio-in-qt
> But it seems so hard to do it. Maybe I can try this later time.
>
>
> Hüseyin
>
>
>
> On Wed, Jul 22, 2009 at 18:06, David Boddie <david.boddie at nokia.com>wrote:
>
>> Huseyin Kozan wrote:
>>
>> > On Wed, Jul 22, 2009 at 13:37, David Boddie <david.boddie at nokia.com>
>> > wrote:
>>
>> > Is this sizeHint() thing for initial size ?
>>
>> Yes. I thought it might influence the height-for-width behavior, but I was
>> mistaken. What you are doing should be correct. However, the height
>> returned
>> by the heightForWidth() function is not treated as an absolute requirement
>> for the size of your widget - the layout won't push other widgets out of
>> the way to make room for your widget.
>>
>> > But I cannot see the difference. What is the requirements for layouting
>> > parents and childs ? I mean we cannot force the main window to maintain
>> > the ratio, and do I need to add spacers near the promoted widget, and/or
>> > child widgets ?
>>
>> You may be able to achieve something like what you want by giving your
>> widget a vertical size policy that lets it expand (Expanding) while giving
>> other widgets in the layout policies (or stretch factors) that ensure that
>> they stay out of the way. Even with these, the window's own size is a
>> constraint you can't override.
>>
>> Another technique for ensuring that some content always has a particular
>> shape is to resize and position it within a widget. So, you look at how
>> much space you have and draw the desired shape, leaving the surrounding
>> area blank. There may be ways to ensure that a standard widget has a
>> certain shape, but that would require a bit more thought on my part.
>>
>> David
>> --
>> David Boddie
>> Senior Technical Writer
>> Nokia, Qt Software
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at trolltech.com
>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090722/0a0d78c2/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: FixedRatioExample.7z
Type: application/octet-stream
Size: 21099 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090722/0a0d78c2/attachment.obj 


More information about the Qt-interest-old mailing list