spraff
10th November 2008, 00:22
Hello. I'm trying to subclass QPushButton to contain arbitrary contents. My first attempt looks a bit like this:
ComplexButton::ComplexButton (QWidget *parent) : QPushButton(Parent) {
QVBoxLayout l = new QVBoxLayout();
l->addWidget(new Foo(this));
l->addWidget(new Bar(this));
l->addWidget(new Baz(this));
setLayout(l);
}
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
setMinimumSize(generic->sizeHint())
Which at least made the button big enough for the contents.
But that still isn't what I want! :o
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.
ComplexButton::ComplexButton (QWidget *parent) : QPushButton(Parent) {
QVBoxLayout l = new QVBoxLayout();
l->addWidget(new Foo(this));
l->addWidget(new Bar(this));
l->addWidget(new Baz(this));
setLayout(l);
}
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
setMinimumSize(generic->sizeHint())
Which at least made the button big enough for the contents.
But that still isn't what I want! :o
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.