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:
{
Q_OBJECT
public:
CQuantityPushButton
(const QString
& label,
QWidget *parent
= 0) : SuperClass
(parent
) {
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
{
SuperClass::paintEvent(event);
}
private:
};
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;
};
To copy to clipboard, switch view to plain text mode
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.
QuantityButton.jpg
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.
Thanks
Stefan
Bookmarks