QErrorMessage 和 QValidator
我正在尝试使用 Qt 中提供的一些小部件来控制我的用户输入。我希望错误消息能够准确显示用户输入的错误,因此我认为使用 switch
语句是最好的。
我的主要项目将获得大量用户输入,感觉应该有一种更简单的方法。 Qt 文档让我相信 QValidator 本质上是一种枚举器数据类型。所以我虽然可以在 switch 语句中使用它,但它似乎并不是一个 int 。
我不确定如何引入 int 值来完成这项工作,而又不会破坏 QValidator 的预期便利性。
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QValidator>
#include <QErrorMessage>
#include <QString>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButtonValidate, SIGNAL(clicked()), this, SLOT(checkData()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::checkData()
{
QValidator *doubleValidator = new QDoubleValidator();
switch(ui->lineEditValidate->setValidator(doubleValidator))
{
case 0:
QErrorMessage *error0 = new QErrorMessage(this);
error0->showMessage("The input is invalid");
break;
case 1:
QErrorMessage *error1 = new QErrorMessage(this);
error1->showMessage("The input is incomplete");
break;
case 2:
break;
default:
QErrorMessage *error = new QErrorMessage(this);
error->showMessage("No input");
}
}
I'm trying to use some of the widgets provided in Qt to control my user input. I want the error message to display exactly what's wrong with the user input so I thought that using a switch
statement would be best.
My main project will get a lot of user input and it feels like there should be an easier way. The Qt documentation lead me to believe that QValidator
is at heart an enumerator data type. So I though I could use it in the switch statement however it doesn't seem to come up as an int.
I'm not sure how to bring in an int value to make this work without defeating the intended convenience of the QValidator.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QValidator>
#include <QErrorMessage>
#include <QString>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButtonValidate, SIGNAL(clicked()), this, SLOT(checkData()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::checkData()
{
QValidator *doubleValidator = new QDoubleValidator();
switch(ui->lineEditValidate->setValidator(doubleValidator))
{
case 0:
QErrorMessage *error0 = new QErrorMessage(this);
error0->showMessage("The input is invalid");
break;
case 1:
QErrorMessage *error1 = new QErrorMessage(this);
error1->showMessage("The input is incomplete");
break;
case 2:
break;
default:
QErrorMessage *error = new QErrorMessage(this);
error->showMessage("No input");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这行代码中,
您尝试打开
setValidator()
函数调用的返回值,该函数调用返回 void。根据我的收集,您似乎想要按照以下方式执行某些操作:
在构造函数中:
以及在
checkData()
中In this line of code
you're attempting to switch on the return value of the
setValidator()
function call, which returns void.From what I gather, it looks like you want to do something along these lines:
In the constructor:
and in
checkData()