PDA

View Full Version : Draw QSpinBox on top of QPushButton



stefanadelbert
27th October 2011, 01:26
I would like to create a custom widget based on a QPushButton with a QSpinBox drawn inside it. Below is the class I currently have it:


class CQuantityPushButton : public QPushButton
{
Q_OBJECT

typedef QPushButton SuperClass;

public:
CQuantityPushButton(const QString& label, QWidget *parent = 0) : SuperClass(parent)
{
spinBox = new QSpinBox(this);
setText(label);

connect(this, SIGNAL(clicked()), this, SLOT(Triggered()));
}
~CQuantityPushButton() {}

signals:
void triggered(int);

private slots:
void
Triggered(int)
{
emit triggered(spinBox->value());
}

protected:
void
CQuantityPushButton::paintEvent(QPaintEvent *event)
{
SuperClass::paintEvent(event);
}

private:
QSpinBox* spinBox;
};

This image shows what I'm actually seeing when the class above is rendered and then it shows a Photoshopped example of what I would like to see.

7040

The way it's currently being drawn (the SpinBox floating above the PushButton) is exactly what I expected to see. But I would like to paint the Spinbox inside the PushButton to the left of the button text - I would like the SpinBox to be part of the PushButton contents.

Could anyone give me some tips? I'm guessing that I'll need to do some work in paintEvent, which is why I have an override in place (which just calls QPushButton::paintEvent for the moment).

For anyone reading this who want more info on Qt widget painting, this seems to be a good article (http://openbooks.sourceforge.net/books/kde20devel/ch04lev1sec2.html).

Thanks
Stefan