PDA

View Full Version : Infinite loop - resize parent from child



bitChanger
4th May 2006, 20:46
For this project i'm using Windows and Qt 4.1.2

I have a QMainWindow that contains a widget that occupies the entire window. When the resizeEvent of the child widget is called I want the child to maintain proportions. After fixing the width/height proportions of the child I need to resize the parent.

I've included a complete program that demonstrates the issue.

Here is the wrong solution, but what would be a good workaround for it?



void MyWidget::resizeEvent(QResizeEvent* event)
{
QSize newsz = event->size();

_figureHeight = newsz.width()*_figureHeight/_figureWidth;
_figureWidth = newsz.width();

parentWidget()->resize(_figureWidth, _figureHeight);
}

jacek
4th May 2006, 21:32
Here's your fish ;)

void MyWidget::resizeEvent( QResizeEvent *event )
{
const QSize size( event->size() );
const int height = size.height();
const int width = size.widgth();
if( ratioW * height == ratioH * width ) {
QWidget::resizeEvent( event );
}
else {
parentWidget()->resize( width, (ratioH * width) / ratioW );
}
}Although it will probably cease to work if you add a menu, status bar or toolbar to your main window.

bitChanger
5th May 2006, 13:12
Thank you.

I beleive that line 10 does not have to be divided by ratioW though.
Changing line 10 to the following seems right to me ??



parentWidget()->resize( width, ratioH * width);

jacek
5th May 2006, 13:21
I beleive that line 10 does not have to be divided by ratioW though.
Changing line 10 to the following seems right to me ??
It depends. In above code ratioW and ratioH can be integers. You can use doubles and then one of there variables can be 1.0 and thus omitted.