keeping a widget to be square
Hello,
I have a problem with placing a widget into a horizontalLayout and making it keep an equal aspect ratio (square).
Here is a rough outline of my code:
Code:
TargetDisplay
::TargetDisplay(QWidget *parent
){
ui.setupUi(this);
d_plot = new RandomPlot(this);
policy.setHeightForWidth(true);
d_plot->setSizePolicy(policy);
ui.horizontalLayout->insertWidget(0, d_plot);
}
and then in my RandomPlot widget I have:
Code:
{
Q_OBJECT
public:
~RandomPlot();
int heightForWidth(int w) const {
return w;
}
}
but my widget is not maintaining an equal aspect ratio. Can somebody please tell me what I'm doing wrong? Thank you!
-James
Re: keeping a widget to be square
This will only work for a widget in a layout. For a top-level window you have to reimplement resizeEvent() and resize the widget to a proper aspect from within.
Re: keeping a widget to be square
Quote:
Originally Posted by
wysota
This will only work for a widget in a layout. For a top-level window you have to reimplement resizeEvent() and resize the widget to a proper aspect from within.
My widget is in HorizontalLayout:
Code:
ui.horizontalLayout->insertWidget(0, d_plot);
Can somebody explain what I need to add to my original code? Thanks
Re: keeping a widget to be square
Nevermind, I figured it out. Instead of adding d_plot directly to the horizontalLayout, I added it to QwtDynGridLayout first, and then added the layout to the horizontalLayout:
Code:
gLayout->addWidget(d_plot);
ui.horizontalLayout->insertLayout(0, gLayout);
Now I get a square
Re: keeping a widget to be square
It's because the stretch factor on your horizontal layout when adding the widget is by default 0.
QHBoxLayout *hbox = new QHBoxLayout();
hbox->addWidget(mywidget, 1);
If you are using Qt Designer, set the layoutStretch parameters.
1 Attachment(s)
Re: keeping a widget to be square
As I found this layout manager that keeps a widgets square, I figured it would be wise to share it:
http://wiki.forum.nokia.com/index.ph...a_widget_in_Qt
Attached a zip file with source and example, in case it goes missing on the wiki.
Thanks to the heads at nokia for making this layout manager :)