Manage the resizing of related widgets
I have a simple problem that is the following:
I have four widgets that are organized in a 2*2 matrix,
the four widgets must have the same size and be squared,
When I resize the window containing the widgets, they must resize themselves maintaining the second constraint, and without going outside the window.
And now the question is "how can I do this?".
I have tried by using QWidget::heightForWidth(int w) function, returning w each time. But, when I only resize in the x direction, the height becomes too large and the widgets go outside the window.
I have tried putting the matrix in a widget and reimplement the resizeEvent() function of this widget. The resizeEvent() is set to compute the expected size of the widgets and to resize them. But in this case I have an infinite recursion and I see the widgets reducing their size and disappearing.
I expected to replace the QWidget::resize() function. But this function is not virtual. Hence I will probably do not have the expected result.
it should be sufficient to listen to the resize event of the window correct?
If so you could do something like this (NOT tested though, but it should give you an idea):
void MyWindow::resizeEvent(QResizeEvent* event)
QSize newSize = event->size(); QSize oldSize = event->oldSize(); if( newSize.width() != oldSize.width() ) this->resize( QSize(newSize.widht(),newSize.width() ); else if( newSize.height() != oldSize.height() ) this->resize( QSize(newSize.height(),newSize.height() );
But this is rather dirty and maybe less ideal solution.
To try. But not in the window itself (do not have to be squared, and contain other elements). May be on a widget containing my matrix. But the aesthetic will probably be not really good. As other widgets will be placed as if the matrix took its full size.
ok this makes the whole thing more tricky...but not impossible. ;)
You can use "QSingleItemSquareLayout":http://developer.nokia.com/Community/Wiki/Archived:Maintaining_square_form_for_a_QWidget. Place you widget which is holding your 4 widgets in the 2x2 grid inside this layout. Then put the layout in your main window where you want it.
Thanks for the link. I will probably not using QSingleItemSquareLayout. But it gives me an idea. Instead of subclassing QLayout I will subclass QWidgetItem by just overridding setGeometry (that is virtual).