c++ - 捕获 QSpinBox 文本字段 mouseEvents

标签 c++ qt mouseevent

我有一个重新实现的 QDoubleSpinBox。我想捕获 mouseDoubleClickEvent 以使用户能够通过 QInputDialog::getDouble() 更改 singleStep

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

谢谢。

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

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

最佳答案

对子进程进行引用的一些技巧,但它有效 =)

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 并为其分配了事件过滤器。在事件过滤器中仅捕获鼠标双击。

关于c++ - 捕获 QSpinBox 文本字段 mouseEvents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11476540/

相关文章:

c++ - 合并数字以形成一个大数字

qt - Qt Creator 的项目文件中是否有特殊的外部文件类别?

java - 在 JPanel 中绘制的圆不会在 mouseClick 的位置绘制

python - 从 C++ 代码自动生成流程图

c++ - 如何管理大型阵列

c++ - Cmake:使用导入的对象

c++ - QtSerialPort:readAll 不产生所有数据,也没有新的 readyRead 信号

c++ - 单击不同窗口上的按钮时更改类的参数

JavaFX:如何平移包含 Button 子项的 ScrollPane?

java - 优化拖动代码