Results 1 to 1 of 1

Thread: Draw QSpinBox on top of QPushButton

  1. #1
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Draw QSpinBox on top of QPushButton

    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:

    Qt Code:
    1. class CQuantityPushButton : public QPushButton
    2. {
    3. Q_OBJECT
    4.  
    5. typedef QPushButton SuperClass;
    6.  
    7. public:
    8. CQuantityPushButton(const QString& label, QWidget *parent = 0) : SuperClass(parent)
    9. {
    10. spinBox = new QSpinBox(this);
    11. setText(label);
    12.  
    13. connect(this, SIGNAL(clicked()), this, SLOT(Triggered()));
    14. }
    15. ~CQuantityPushButton() {}
    16.  
    17. signals:
    18. void triggered(int);
    19.  
    20. private slots:
    21. void
    22. Triggered(int)
    23. {
    24. emit triggered(spinBox->value());
    25. }
    26.  
    27. protected:
    28. void
    29. CQuantityPushButton::paintEvent(QPaintEvent *event)
    30. {
    31. SuperClass::paintEvent(event);
    32. }
    33.  
    34. private:
    35. QSpinBox* spinBox;
    36. };
    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
    Last edited by stefanadelbert; 27th October 2011 at 06:58.

Similar Threads

  1. Replies: 10
    Last Post: 10th February 2011, 23:31
  2. Draw button(similer to QPushButton) using the QPainter.
    By pastispast in forum Qt Programming
    Replies: 3
    Last Post: 24th July 2010, 20:48
  3. Replies: 0
    Last Post: 22nd February 2010, 09:30
  4. QSpinbox
    By sonuani in forum Newbie
    Replies: 3
    Last Post: 21st March 2008, 10:36
  5. Replies: 3
    Last Post: 26th September 2006, 12:16

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.