Disable selection of numbers in a QSpinBox



  • I have a heavily styles QSpinBox for an embedded system (touchscreen only), and I have everything looking the way I want. The problem is that the device has a touchscreen and the users might click in the text area, or worse drag. This produces both a cursor and the numbers become highlighted.

    I have a hokey solution for the highlighting, change the highlight color to transparent, and I'd like a better solution for this that hopefully also addresses having the cursor show on the screen.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Would setReadOnly(true) help in that case ?



  • no because the spinbox becomes unusable, the user can no longer click the buttons to change the control's value. futher, readonly controls still allow for highlighting so that you can copy (a read function).


  • Lifetime Qt Champion

    Then you could consider subclassing QSpinBox and provide a custom QLineEdit that has the behavior you want



  • That's what I was afraid of. Never done that before. Any good examples?


  • Lifetime Qt Champion

    @
    MySpinBox::MySpinBox(QWidget *parent):
    QSpinBox(parent)
    {
    setLineEdit(new MyLineEdit);
    }

    MyLineEdit::MyLineEdit(QWidget *parent):
    QLineEdit(parent)
    {
    setReadOnly(true);
    }

    void MyLineEdit::mousePressEvent(QMouseEvent *)
    {}

    void MyLineEdit::mouseMoveEvent(QMouseEvent *)
    {}

    void MyLineEdit::mouseReleaseEvent(QMouseEvent *)
    {}
    @

    WARNING: Not tested but it should do the trick, there are other ways to do it (e.g. a simple event filter so you only need to subclass QSpinBox)

    Hope it helps


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.