Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to modify QSpinBox appearance



  • Hi All,

    I have got a basic question. How to sublcass a Qt object to make some modification in the appearance?

    For ex. I need to modify the standard QSpinBox just with the increment/decrement buttons without the value display:

    d7d49234-77dc-4de4-b5c1-dbd0f422bdf5-image.png

    Even though I came across the keyword "sublcass" an object, I never understood systematic way of doing this. It will be very helpful if someone shows the step-by-step procedure to subclass a standard QSpinBox. I started with the following steps, correct me if I'm wrong

    1. Create a class called customspinbox with base class QSpinBox (include QObject and QWidget).
    2. Did following modifications to the customspinbox class:
    #ifndef CUSTOMSPINBOX_H
    #define CUSTOMSPINBOX_H
    
    #include <QObject>
    #include <QWidget>
    #include <QSpinBox>
    
    class customspinbox : public QSpinBox
    {
      Q_OBJECT
      
    public:
      customspinbox(QWidget *parent =nullptr);
    };
    
    #endif // CUSTOMSPINBOX_H
    
    1. In the mainwindow.ui I added a SpinBox and promoted this to the customspinbox class

    2. I speculate the next step will be to override paintEvent function to delete the textBrowser or similar object which displays the spinbox value. But I don't know how to do this.

    can you show me how to get rid of the display part of the QSpinBox?


  • Lifetime Qt Champion

    Hi,

    Wouldn't it be simpler to create a custom widget with these two buttons ?
    It looks like you are about to make your life more complicated than needed.


Log in to reply