PDA

View Full Version : Problem with Size Policy of derived QWidget



eehmke
9th November 2008, 16:19
Hello,
I wrote a class following the letter-envelop idiom:
http://en.wikibooks.org/wiki/More_C++_Idioms/Envelope_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

caduel
9th November 2008, 19:45
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.

eehmke
9th November 2008, 23:23
> 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();
}

caduel
9th November 2008, 23:48
you need to set a layout on the containing widget (ie your 'envelope').


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

HTH

eehmke
10th November 2008, 08:55
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!

caduel
10th November 2008, 09:18
hint: margins (in QLayout)

eehmke
12th November 2008, 14:43
problem solved. Thanks again