捕获QSpinBox文本字段mouseEvents

时间:2012-07-13 18:49:41

标签: c++ qt mouseevent

我有一个重新实现的QDoubleSpinBox。我希望抓住mouseDoubleClickEvent以允许用户通过singleStep更改QInputDialog::getDouble()

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

感谢。

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

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

2 个答案:

答案 0 :(得分: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 ,并为其分配了事件过滤器。在事件过滤器中只捕获鼠标双击。

答案 1 :(得分: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);
}