识别 QLineEdit 是否失去焦点

Ale*_*ph0 2 qt focus qlineedit qvalidator

在我的一个项目中,我有一系列QLineEdit小部件,它们应该接受位于特定范围内的双数。由于某些原因我无法使用QDoubleSpinBox

现在我用来QDoubleValidator检查我的号码是否在给定范围内。不幸的是,只有在给出editingFinished时才会发出信号。QValidatorQValidator::Acceptable

现在假设我可能有一系列这样的QLineEdit小部件。在其中一个小部件中,我输入了错误的数字,然后将焦点切换到另一个小部件。用户在QLineEdit.

相反,我想要的行为是将焦点设置到包含错误输入的小部件并发出警告。

由于某些原因,我未能实现此功能。即使在我查阅了 Qt 文档之后。

这是我的完整代码。

ValidatedDoubleEditorWidget.h

#pragma once

#include <QWidget>

namespace Ui {
    class validatedDoubleEditorWidget;
}

class QDoubleValidator;

    class ValidatedDoubleEditorWidget : public QWidget {
        Q_OBJECT
    public:
        ValidatedDoubleEditorWidget(double min, double max, double value);

        double getValue() const;
        void setValue(const double value);

    private slots:
        void on_lineEdit_editingFinished();

    protected:
        virtual void focusOutEvent(QFocusEvent *event) override;


        virtual void focusInEvent(QFocusEvent *event) override;

    private:
        Ui::validatedDoubleEditorWidget* mWidget = nullptr;
        double mValue = 0.;
        double mMin = 0.;
        double mMax = 0.;
        QDoubleValidator* mValidator = nullptr;

    };
Run Code Online (Sandbox Code Playgroud)

ValidatedDoubleEditorWidget.cpp

#include "ValidatedDoubleEditorWidget.h"
#include <QDoubleValidator>
#include <QMessageBox>
#include <QDebug>
#include "ui_ValidatedDoubleEditorWidget.h"

ValidatedDoubleEditorWidget::ValidatedDoubleEditorWidget(double min, double max, double value)
{
    mWidget = new Ui::validatedDoubleEditorWidget;
    mWidget->setupUi(this);
    mValue = value;
    mWidget->lineEdit->setText(QString("%1").arg(value));
    mValidator = new QDoubleValidator(min, max, 20, this);
    mWidget->lineEdit->setValidator(mValidator);
    setFocusProxy(mWidget->lineEdit);
    setFocusPolicy(Qt::StrongFocus);
}

double ValidatedDoubleEditorWidget::getValue() const
{
    return mValue;
}

void ValidatedDoubleEditorWidget::setValue(const double value)
{
    mValue = value;
    mWidget->lineEdit->setText(QString("%1").arg(value));
}


void ValidatedDoubleEditorWidget::on_lineEdit_editingFinished()
{
    QString text = mWidget->lineEdit->text();
    qDebug() << "Editing finished";
    bool ok;
    double value = text.toDouble(&ok);
    if (!ok) {
        //
    }
    else {
        mValue = value;
    }
}

void ValidatedDoubleEditorWidget::focusOutEvent(QFocusEvent *event)
{
    qDebug() << "OutFocus";
    QString text = mWidget->lineEdit->text();
    int i;
    auto state=mValidator->validate(text, i);
    if (state != QValidator::Acceptable) {
        QMessageBox::warning(this, tr("Invalid Input!"), tr("Please check your input."), QMessageBox::Ok); 
        mWidget->lineEdit->setText(QString("%1").arg(mValue));
        mWidget->lineEdit->setFocus();
    }

}

void ValidatedDoubleEditorWidget::focusInEvent(QFocusEvent *event)
{
    qDebug() << "InFocus";
}
Run Code Online (Sandbox Code Playgroud)

测试运行程序.cpp

#include <QApplication>
#include <QMap>
#include <QFrame>
#include <QHBoxLayout>
#include "ValidatedDoubleEditorWidget.h"

int main(int argc, char** args) {
    QApplication app(argc, args);
    QFrame frame;
    frame.setLayout(new QHBoxLayout);
    frame.layout()->addWidget(new ValidatedDoubleEditorWidget(-1., 4., 1.));
    frame.layout()->addWidget(new ValidatedDoubleEditorWidget(-2., 4., 5.));

    frame.show();
    app.exec();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

aat*_*two 5

SetFocusProxy() 并不意味着父窗口小部件将获得子窗口小部件的焦点事件。这意味着当父窗口部件获得焦点时,子窗口部件将获得焦点。

因此,在您的情况下,您的 focusIn() 和 focusOut() 事件处理程序适用于父窗口小部件,但父窗口小部件已被赋予行编辑作为其焦点代理,这意味着行编辑将是您要检查焦点的窗口小部件,并且出事件。

检测行编辑的相关焦点进入和焦点离开事件的一种方法是使用事件过滤器。基本上,WidgetA 可以在WidgetB 上安装事件过滤器,然后当WidgetB 将要查看事件(例如聚焦或聚焦事件)时收到通知。

因此,在您的情况下,您可以在 ValidatedDoubleEditorWidget 类的构造函数中的行编辑上安装事件过滤器,如下所示:

mWidget->lineEdit->installEventFilter( this );
Run Code Online (Sandbox Code Playgroud)

然后您可以添加事件过滤器实现(再次添加到您的 ValidatedDoubleEditorWidget 类中):

// Declaration
virtual bool eventFilter(QObject *watched, QEvent *event) override;

// Implementation
bool ValidatedDoubleEditorWidget::eventFilter(QObject *watched, QEvent *event)
{
    if( event->type() == QEvent::FocusIn ) {

        qDebug() << "Line edit focus in event";
    }

    else if( event->type() == QEvent::FocusOut ) {

        qDebug() << "Line edit focus out event";
    }
    return false; // We return false to ignore the event and allow the child to recieve the event normally
}
Run Code Online (Sandbox Code Playgroud)

这应该会让您能够检测到 ValidatedDoubleEditorWidget 何时失去焦点并且不包含有效输入。

  • 这实际上是一个很好的答案,它解决了我遇到的问题(当用户单击包含预定义字符串的“QListWidget”时,“QLineEdit”失去焦点和选择),而无需子类化“QLineEdit”。我总是忘记`installEventFilter()`!谢谢! (2认同)