Results 1 to 3 of 3

Thread: Simple custom widget won't size properly

  1. #1
    Join Date
    Mar 2007
    Thanked 1 Time in 1 Post
    Qt products

    Default Simple custom widget won't size properly


    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?


  2. #2
    Join Date
    Feb 2006
    Thanked 541 Times in 521 Posts
    Qt products
    Unix/X11 Windows

    Default Re: Simple custom widget won't size properly

    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.


  3. #3
    Join Date
    Jan 2006
    Warsaw, Poland
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits

    Default Re: Simple custom widget won't size properly

    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().

Similar Threads

  1. Custom tab widget question
    By PrimeCP in forum Qt Programming
    Replies: 2
    Last Post: 7th August 2007, 11:17
  2. Custom Shape Widget (resize)
    By PiXeL16 in forum Qt Programming
    Replies: 7
    Last Post: 12th February 2007, 07:00
  3. Problem applying setWindowOpacity to a custom Widget
    By yellowmat in forum Qt Programming
    Replies: 8
    Last Post: 1st November 2006, 10:05
  4. Replies: 3
    Last Post: 12th April 2006, 08:20
  5. Replies: 4
    Last Post: 24th March 2006, 22:50


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.