View Full Version : Simple custom widget won't size properly

6th August 2007, 22:56

I wanted to create an LED widget. Something very
simple that I could drag from my QT Designer
widget Box.

I created a widget form and placed a QLabel
on it. This Qlabel has a stylesheet associated
with it that gives it the look and feel that I
want. I have constrained it to be of fixed size
20 X 12.

If I preview it in QT Designer it looks fine.

I built my plugin and copied the necessary
files to the necessary places and I see my
custom widget in the toolbox.

I now create a new form and drag my custom
widget onto the form. It appears but...

1. The initially displayed rectangle size is not what I wanted - 20 X 16
and the sizePolicy is shown as Preferred,Preferred.
I don't see the actual LED unless I increase
the size to about 51 X 31. At that point
the LED appears but is not centered. At a
size of 41 X 51 the LED is centered.

I cannot get the size of the control down
to where I want it.

I also have specified an initial size in my domXml() file:

QString OvalLEDPlugin::domXml() const
return "<widget class=\"OvalLED\" name=\"ovalLED\">\n"
" <property name=\"geometry\">\n"
" <rect>\n"
" <x>11</x>\n"
" <y>14</y>\n"
" <width>20</width>\n"
" <height>12</height>\n"
" </rect>\n"
" </property>\n"

Why can't I get this seemingly simple widget to be sized as I specified in the .ui file?


6th August 2007, 23:10
Answer to 1&2:
You have to create a layout for the widget that contains that styled label;
Horizontal layout will do. Make sure you set the spacing and margins for it to 0 - you don't want to waste any space.

Most likely the lack of a layout is what is making your custom widget not respecting sizes and positions.


9th August 2007, 13:12
If you want a widget of a particular size, reimplement its sizeHint() and minimumSizeHint() and return QSize(20,12) there. No need to touch domXml().