QDoubleSpinBox如何双击标记整个文本?

How to mark the whole text by double clicking for QDoubleSpinBox?

我有一个继承自 QDoubleSpinBox 的 class。

 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 );
   ..........
 }

双击进入编辑区的结果是这样的,只标记了组分隔符之间的部分。如果我三次点击,整个文本就会被标记。

我应该怎么修改,让我双击进入编辑区时(无论整数部分还是小数部分),整个文本都被标记?

解决方案是重新实现 QLineEdit::mouseDoubleClickEvent 方法(不是 QDoubleSpinBox::mouseDoubleClickEvent)。

自定义行编辑:

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);
}

然后将其设置为您的自定义旋转框

NumericEdit::NumericEdit(QWidget *p_parent) : QDoubleSpinBox(p_parent)
{
    //...
    ExtendedLineEdit* lineEdit = new ExtendedLineEdit(this);
    setLineEdit(lineEdit);
}