How can I save proporties of QWidget while resizing?
For example I want QWidget's height to be always 3/4 of width.
Thanks in advance!
How can I save proporties of QWidget while resizing?
For example I want QWidget's height to be always 3/4 of width.
Thanks in advance!
I don't an easy option for that, but you can reimpl QWidget::resizeEvent ( QResizeEvent * event ) and fix the proportion yourself.
Can you give me an example?
I tried something like this:But as I remember there wasn't even any difference...Qt Code:
e->size()->setWidth(...); e->size()->setHeight(...); }To copy to clipboard, switch view to plain text mode
Macok (28th March 2009)
Thanks, but how should I reimplement that QWidget::heightForWidth()?
I tried this:What's wrong?Qt Code:
return w*3/(double)4; }To copy to clipboard, switch view to plain text mode
That's not enough. You also have to tell the layout (through size policy) that you want to use heightForWidth. I'm also not sure if you can actually set it on a top level widget (you might have to set it on a child). Also remember this controlls the preferred width/height dependency. If you want to force it, you have to set a size constraint on the widget's layout. So if you don't know how to do that, reimplementing resizeEvent() might be a better (or at least faster to implement) idea. Your current implementation didn't work because QResizeEvent::size() returns a copy of the size object and after you modify the copy you need to set it back on the widget. It will work if you implement it properly.
I tried, but it still doesn't work.
Here's the code for 1:1 proporties:If width is bigger than height, i set height to be the same as width and vice versa.Qt Code:
QResizeEvent *event; if(e->size().width()<e->size().height()){ resize(e->size().height(), e->size().height()); } else{ resize(e->size().width(), e->size().width()); } }To copy to clipboard, switch view to plain text mode
What's wrong?
Why are you creating a new resize event?
Please, if you use some method (like resizeEvent) please read its docs first. It's clearly stated the event is delivered after the widget has already been resized so changing the data sent to the base implementation won't do you any good especially that QWidget's resizeEvent() is empty.
Qt Code:
if(re->width()==re->height()) return; // avoid infinite loop int larger = qMax(re->width(), re->height()); resize(larger, larger); }To copy to clipboard, switch view to plain text mode
Macok (29th March 2009)
Thanks, now it's ok.
If I don't callQGLWidget::resizeEvent(...) QGLWidget will not scale the scene displayed in it.Why are you creating a new resize event?
Finally, this works fine:But I have one more question.Qt Code:
if(re->size().width()==re->size().height()) return; // avoid infinite loop int min = qMin(re->size().width(), re->size().height()); resize(larger, larger); }To copy to clipboard, switch view to plain text mode
When width of my window is bigger than height, widget will not be expanded on the whole window, but it'll be placed on the left side.
I'd like to place widget exactly in the middle of window's width.
Is there any simple way to reach this effect?
Maybe something with setGeometry()?
Thanks in advance.
You have a memory leak. As for the question, you already answered it.
No, I didn't.
Maybe you misunderstood my question.
I have widget with proporties 1:1, but window when this widget is placed has another proporties, so widget is placed on the left side, or top of the window.
I'd like to place it in the middle, but I don't know how to do it.
I tried to use spacers but without results.
setGeometry() or move()...to desired location..shouldnt be hard
Maybe I have.
So your GL widget is not a top-level window, right? In that case you should have used heightForWidth...I have widget with proporties 1:1, but window when this widget is placed has another proporties, so widget is placed on the left side, or top of the window.
Anyway... you don't need spacers although of course you can use them. When you place your widget in a layout, make it a grid layout and when calling addWidget() you can pass an alignment flag. But if you are using Designer to design your window then spacers are an easier way of obtaining the result. Just remember you'll have to decide which direction you want "anchored" to the window - you can't use four spacers or else your GL widget won't resize at all, you have to place two spacers either on the left and right of the widget or above and below it.I'd like to place it in the middle, but I don't know how to do it.
I tried to use spacers but without results.
On second thought you might get away with four spacers but you need to set the size policy of your GL widget to Expanding in both directions. If you can accept that then it should work (provided it won't break your aspect ratio).
Macok (29th March 2009)
Bookmarks