2

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");
    }
}
Wylie Coyote SG.
  • 1,009
  • 6
  • 22
  • 37

1 Answers1

2

In this line of code

switch(ui->lineEditValidate->setValidator(doubleValidator))

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:

ui->setupUi(this);
connect(ui->pushButtonValidate, SIGNAL(clicked()), this, SLOT(checkData()));
QValidator *doubleValidator = new QDoubleValidator();
ui->lineEditValidate->setValidator(doubleValidator);

and in checkData()

int pos = 0;
switch(ui->lineEditValidate->validator()->validate(ui->lineEditValidate->text(), pos))
{
case QValidator::Invalid:
...
case QValidator::Incomplete:
...
case QValidator::Invalid:
...
}
Chris
  • 17,119
  • 5
  • 57
  • 60
  • I think you have defiantly got me on the right track here but I'm getting a complier issue for the switch statment: no matching function for call to QValidator::validate (QString) const candidate is: virtual QValidator::State QValidator::validate(QString&, int&) const it says the candidate expects two arguments and only one is provided. I was looking over this and the documentation isn't to clear as to what should go here. Any Ideas? I dont know what else it wants in the validate parameters. I was thinking parent or this but neither would work in the function. I get the same compile error. – Wylie Coyote SG. Jan 03 '12 at 22:40
  • I edited the statement to be more correct. You're really going to need to learn to rely on reading the [Qt documentation](http://developer.qt.nokia.com/doc/qt-4.8/) for these kinds of things to really understand what's happening – Chris Jan 03 '12 at 23:00
  • Thanks; Your right, the QT documentation is something I have been struggling with. Its been an uphill march but Ill get there. Thanks for your help! – Wylie Coyote SG. Jan 04 '12 at 00:04