Results 1 to 7 of 7

Thread: Problem with Size Policy of derived QWidget

  1. #1
    Join Date
    Nov 2008
    Location
    Berlin, Germany
    Posts
    13
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt/Embedded PyQt3 PyQt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Problem with Size Policy of derived QWidget

    Hello,
    I wrote a class following the letter-envelop idiom:
    http://en.wikibooks.org/wiki/More_C+...nvelope_Letter
    where the class is derived from QWidget and contains a letter class of QWidget*. I need this class as a base class for several other widgets like mylabel, mycheckbox. I created a sizeHint method like this:

    QSize my_label::sizeHint () const {
    return ((QLabel*)letter)->sizeHint();
    }

    This works so far, but one problem is unsolved: the labels are rendered using the minimum size, leaving different amount of white space at the right side. I want them to fill the space in the layout with the background color, as it did when I used plain QLabel widgets. What would be the right approach?
    Eggert

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with Size Policy of derived QWidget

    if you add widgets to a vertical layout they will use the available space (horizontally).

    show us some more code of what you are doing, so we do not have to guess.

  3. #3
    Join Date
    Nov 2008
    Location
    Berlin, Germany
    Posts
    13
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt/Embedded PyQt3 PyQt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Problem with Size Policy of derived QWidget

    > if you add widgets to a vertical layout they will use the available space
    > (horizontally).
    That may be the problem. In the containing dialog, the widget is added to the layout:
    layout_edit->addWidget (label[i], i, 0);
    where label[i] is of the class described (my_label). But that widget is only a container class that contains the real widget. So what methods must I forward to the contained widget?

    Here are some details:

    class my_widget: public QWidget{ private:
    Q_OBJECT

    public:
    sk_widget (QWidget *parent);
    virtual QSize sizeHint () const = 0;
    virtual void setAutoFillBackground (bool) = 0;

    protected:
    QWidget* letter;
    };

    my_label::my_label (const QString &text, QWidget *parent)
    :my_widget (parent)
    {
    letter = new QLabel (text, this);
    }

    QSize my_label::sizeHint () const {
    return ((QLabel*)letter)->sizeHint();
    }

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with Size Policy of derived QWidget

    you need to set a layout on the containing widget (ie your 'envelope').

    Qt Code:
    1. my_label::my_label (const QString &text, QWidget *parent)
    2. :my_widget (parent)
    3. {
    4. letter = new QLabel(text);
    5. l->addWidget(this);
    6. setLayout(l);
    7. }
    To copy to clipboard, switch view to plain text mode 

    HTH

  5. #5
    Join Date
    Nov 2008
    Location
    Berlin, Germany
    Posts
    13
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt/Embedded PyQt3 PyQt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Problem with Size Policy of derived QWidget

    Yes, that was it! I only had to modify:

    my_label::my_label (const QString &text, QWidget *parent)
    :my_widget (parent)
    {
    QVBoxLayout *l = new QVBoxLayout;
    letter = new QLabel(text);
    l->addWidget(letter);
    setLayout(l);
    }

    That works, only that now the labels take too much vertical space. That can't be so hard. Thanks!

  6. #6
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with Size Policy of derived QWidget

    hint: margins (in QLayout)

  7. The following user says thank you to caduel for this useful post:

    eehmke (12th November 2008)

  8. #7
    Join Date
    Nov 2008
    Location
    Berlin, Germany
    Posts
    13
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt/Embedded PyQt3 PyQt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Problem with Size Policy of derived QWidget

    problem solved. Thanks again

Similar Threads

  1. Size Policy Problem
    By SenSej in forum Newbie
    Replies: 0
    Last Post: 28th October 2008, 14:13
  2. QLabel size policy
    By Caius Aérobus in forum Qt Programming
    Replies: 3
    Last Post: 7th December 2007, 17:57

Bookmarks

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.