1

我有一个重新实现的 QDoubleSpinBox。我想mouseDoubleClickEvent通过singleStep.QInputDialog::getDouble()

我的问题是,当我重新实现时,mouseDoubleClickEvent我只捕捉到发生在箭头按钮上的双击。我实际上想忽略箭头中发生的双击,只捕获文本字段中发生的双击。我有一种感觉,我需要重新实现 QDoubleSpinBox 子级的 mouseDoubleClickEvent,但我不确定如何重新实现子级事件或如何选择正确的子级 请参阅我在代码中限制子级 QRect 的尝试:我认为我需要指定哪个孩子...?

谢谢。

编辑:更正了类声明/定义名称不匹配。

MyQDoubleSpinBox.h

class MyQDoubleSpinBox : public QDoubleSpinBox
{
    Q_OBJECT

public:
    MyQDoubleSpinBox(QString str, QWidget *parent = 0);
    ~MyQDoubleSpinBox();

public slots:
    void setStepSize(double step);

private:
    double stepSize;
    QString name;

protected:
    void mouseDoubleClickEvent(QMouseEvent *e);
};

MyQDoubleSpinBox.cpp

#include "MyQDoubleSpinBox.h"

MyQDoubleSpinBox::MyQDoubleSpinBox(QString str, QWidget *parent)
    :   QDoubleSpinBox(parent), stepSize(1.00), name(str)
{
    this->setMinimumWidth(150);
    this->setSingleStep(stepSize);
    this->setMinimum(0.0);
    this->setMaximum(100.0);
}

MyQDoubleSpinBox::~MyQDoubleSpinBox()
{

}

void MyQDoubleSpinBox::setStepSize(double step)
{
    this->setSingleStep(step);
}

void MyQDoubleSpinBox::mouseDoubleClickEvent(QMouseEvent *e)
{
    if( this->childrenRect().contains(e->pos()) )
    {
        bool ok;
        double d = QInputDialog::getDouble(this,
            name,
            tr("Step Size:"),
            this->singleStep(),
            0.0,
            1000.0,
            2,
            &ok);

        if(ok)
            this->setSingleStep(d);
    }
}
4

2 回答 2

1

有点技巧让孩子参考,但它有效=)

MyQDoubleSpinBox.h:

class MyQDoubleSpinBox : public QDoubleSpinBox
{
    Q_OBJECT

public:
    MyQDoubleSpinBox(QString str, QWidget *parent = 0);
    ~MyQDoubleSpinBox();

public slots:
    void setStepSize(double step);

private:
    double stepSize;
    QString name;

protected:
    bool eventFilter(QObject *, QEvent *e);
};

MyQDoubleSpinBox.cpp

MyQDoubleSpinBox::MyQDoubleSpinBox(QString str, QWidget *parent)
    :   QDoubleSpinBox(parent), stepSize(1.00), name(str)
{
    this->setMinimumWidth(150);
    this->setSingleStep(stepSize);
    this->setMinimum(0.0);
    this->setMaximum(100.0);
    QLineEdit *editor = this->findChild<QLineEdit *>("qt_spinbox_lineedit");
    editor->installEventFilter(this);
}

MyQDoubleSpinBox::~MyQDoubleSpinBox()
{

}

void MyQDoubleSpinBox::setStepSize(double step)
{
    this->setSingleStep(step);
}

bool MyQDoubleSpinBox::eventFilter(QObject *, QEvent *e)
{
    if (e->type() == QMouseEvent::MouseButtonDblClick)
    {        bool ok;
        double d = QInputDialog::getDouble(this,
                                           name,
                                           tr("Step Size:"),
                                           this->singleStep(),
                                           0.0,
                                           1000.0,
                                           2,
                                           &ok);

        if(ok)
            this->setSingleStep(d);
    }
    return false;
}

我没有覆盖事件,而是获得了底层QLineEdit的引用并为其分配了事件过滤器。在事件过滤器中仅捕获鼠标双击。

于 2012-07-13T20:48:36.823 回答
0

您可以使用 this->lineEdit() 直接访问 QLineEdit

MyQDoubleSpinBox::MyQDoubleSpinBox(QString str, QWidget *parent)
    :   QDoubleSpinBox(parent), stepSize(1.00), name(str)
{
    this->setMinimumWidth(150);
    this->setSingleStep(stepSize);
    this->setMinimum(0.0);
    this->setMaximum(100.0);
    QLineEdit *editor = this->lineEdit(); // change here
    editor->installEventFilter(this);
}
于 2020-02-13T10:46:06.320 回答