1

I have a class which inherit from QDoubleSpinBox.

 class NumericEdit : public QDoubleSpinBox
 {      
 public:
   NumericEdit( QWidget *p_parent = nullptr );

 protected:
   bool event( QEvent *p_event ) override;
   void keyPressEvent( QKeyEvent *p_event ) override;
   void keyReleaseEvent( QKeyEvent *p_event ) override;
   void focusInEvent( QFocusEvent *p_event ) override;
   void focusOutEvent( QFocusEvent *p_event ) override;
   ............
 };

 NumericEdit::NumericEdit( QWidget *p_parent ) : QDoubleSpinBox( p_parent )
 {
   initStyleSheet();
   setButtonSymbols( QAbstractSpinBox::NoButtons );
   setGroupSeparatorShown( true );
   ..........
 }

The result when I double click into the editing field is like this, only the part in between group separators is marked. If I triple click, the whole text is then marked.

enter image description here

How should I change, sothat when I double click into the editing field (no matter in integer part or decimal part), the whole text is marked?

enter image description here

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
songvan
  • 369
  • 5
  • 21
  • I think this is normal behaviour. If you want it to behave the way you want, you have to override mouseDoubleClickEvent – George Jun 04 '19 at 08:08
  • @George: I thought about that already. But what should be in the function `mouseDoubleClickEvent`, i still do not know – songvan Jun 04 '19 at 08:46

1 Answers1

4

Solution is reimplementing of QLineEdit::mouseDoubleClickEvent method (not QDoubleSpinBox::mouseDoubleClickEvent).

Custom line edit:

class ExtendedLineEdit : public QLineEdit
{
    Q_OBJECT
public:
    explicit ExtendedLineEdit(QWidget *parent = nullptr);
protected:
    void mouseDoubleClickEvent(QMouseEvent *event);
}

void ExtendedLineEdit::mouseDoubleClickEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        selectAll();
        event->accept();
        return;
    }

    QLineEdit::mouseDoubleClickEvent(event);
}

And then set it to your custom spin box

NumericEdit::NumericEdit(QWidget *p_parent) : QDoubleSpinBox(p_parent)
{
    //...
    ExtendedLineEdit* lineEdit = new ExtendedLineEdit(this);
    setLineEdit(lineEdit);
}
Serhiy Kulish
  • 1,057
  • 1
  • 6
  • 8
  • I have one more question. Why do we override `mouseDoubleClickEvent` of QLineEdit, not QDoubleSpinBox? – songvan Jun 04 '19 at 08:58
  • 3
    @songvan Because internal `QLineEdit` of `QDoubleSpinBox` accept event before it rich `QDoubleSpinBox`. So if you reimplement `QDoubleSpinBox::mouseDoubleClickEvent` you can catch this event only on double clicking on arrows (up/down) not in internal line edit. Also `QLineEdit::mouseDoubleClickEvent` can avoid double clicking on uneditable parts of `QDoubleSpinBox` (like buttons, borders, background etc). – Serhiy Kulish Jun 04 '19 at 09:43