私のプロジェクトの 1 つにQLineEdit
、特定の範囲内にある 2 つの数値を受け入れる一連のウィジェットがあります。諸事情により使えませんQDoubleSpinBox
。
今、私はQDoubleValidator
自分の番号が指定された範囲内にあるかどうかを確認するために使用しています。残念ながら、シグナルは が与えられたeditingFinished
場合にのみ発信されます。QValidator
QValidator::Acceptable
ここで、一連のそのようなQLineEdit
ウィジェットがあるとします。1 つでは、間違った数字を入力してから、フォーカスを別のウィジェットに切り替えます。ユーザーには、 内に不適切な値が残されます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;
};
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";
}
TestRunner.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;
}