Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Custom drawn QSpinBox widget
Qt 6.11 is out! See what's new in the release blog

Custom drawn QSpinBox widget

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello,

    I'd like to extend QSpinBox/QDoubleSpinBox drawing so that some extra graphics is put to the right of the original spinbox. This extra content is outside of the default rectangle box of the spinbox (red area):

    example image

    Deriving directly from QSpinBox saves me from re-defining/declaring a lot of set/get methods. All I need to do is to override the widget's size methods and the paintEvent() method.

    I struggle with getting the correct size for the widget: If I define a size of 60x20 pixel for the whole widget (spinbox + extra content), only 40 pixel should be used for the spinbox.

    QSize
    MySpinBox::sizeHint() const {
    
    	QSize size = QDoubleSpinBox::sizeHint();
    	size.rwidth() += 20;   // extra size for additional content
    	qDebug() << "Hint:" << size;
    	return size;
    }
    
    void 
    MySpinBox::resizeEvent(
    		QResizeEvent		* event) {
    
    	QSize newSize = event->size();
    	newSize.rwidth() -= 20;
    	QResizeEvent	event2(newSize, event->oldSize());
    	QDoubleSpinBox::resizeEvent(&event2);
    }
    

    It seems my trivial approach is not getting me anywhere. Can anyone lead me into the right direction about where to look for composition of original QWidget with custom widgets?

    Thank you for reading!
    Paule

    Btw; When overriding paintEvent() with empty method, the edit box and its background still gets drawn. I wonder why that's the case:

    void
    MySpinBox::paintEvent(
    		QPaintEvent		* event) {
       // nothing
    }
    
    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      You safe yourself a lot of trouble if you just compose a new widget instead integrating it into a QSpinBox derived one.
      One big problem which comes to my mind would be the mouse-click check done by QSpinBox internally. Which is performed using it's QStyle. Since you "resize" the widget in the size hint this wouldn't work anymore. You would need to adapt the QStyleOption and always adapt the rect in it to match the actual spinbox. Which is not possible to guarantee in all situations.

      So i suggest you to go like this:
      Create a container widget (derived from QFrame for example). Now use QLayouts to compose the spinbox beside a QLabel. Or place the SpinBox in the container manually (call setGeometry on every resizeEvent) and paint the rectangle beside the spinbox in the container widget's paintEvent handler.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Raven-worx,

        thank you for the reply.

        I will use a composed widget then. Unfortunately all access methods of QSpinBox are hidden from now on. Is there any chance I dont have to re-define them in the composite widget class?

        Regards,
        Paule

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          just provide a getter which returns the pointer to the spinbox and make your calls on it directly.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved