Hello. I'm trying to subclass QPushButton to contain arbitrary contents. My first attempt looks a bit like this:

Qt Code:
  1. ComplexButton::ComplexButton (QWidget *parent) : QPushButton(Parent) {
  2.  
  3. l->addWidget(new Foo(this));
  4. l->addWidget(new Bar(this));
  5. l->addWidget(new Baz(this));
  6. setLayout(l);
  7.  
  8. }
To copy to clipboard, switch view to plain text mode 

Unfortunately the button is a minimal size and the contents are squashed. I tried putting the QVBoxLayout in a generic QWidget and adding the line
Qt Code:
  1. setMinimumSize(generic->sizeHint())
To copy to clipboard, switch view to plain text mode 
Which at least made the button big enough for the contents.

But that still isn't what I want!

I want the buttons to expand their width to fill their parent, causing the parent to widen if necessary. This would happen automatically if I hadn't subclassed QPushButton but merely put a long string in it. I can't recreate that behaviour for love nor money. I've been playing around with the size policy but that hasn't helped and I don't readlly understand it. I imagine the trick is with QPushButton's default layout which I'm replacing, but calling layout() returns a null pointer, and now I'm thoroughly confused.

To sum up: I want a subclass of QPushButton with an arbitrary widget as contents and a space-filling width behaviour.

Clue, please? Cheers.