I'm a student programmer and 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 whats wrong with the user input so I thought that using a switch statement would be best. I may not be correct about this and if there is a better way of doing this I am all ears! My main project will get allot 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.
Any info on how to make this work or do it better would be greatly appreciated. Thanks in advance.
#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");
}
}