PDA

View Full Version : qdoublevalidator and plaintext



daniel_muller
3rd October 2013, 12:58
Hi,
Please, I am a very beginner in qt and this must be a stupid question. I am using a plainTextEdit box to input a list of double precision numbers. Is it possible to use QDoubleValidator to forbid the input of other characters (e.g. letters, symbols #$%?, etc. ) into the TextEdit box? In the following I put the c++ code.
Thank you,
Daniel
-------------------------------------------------

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <stdio.h>
#include <math.h>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_released()
{

//double x;
QStringList x;
int i,j,k=0;
//int i=1; int j;
//x=ui->plainTextEdit->toPlainText().toDouble();
// QDoubleValidator *x;
x = ui->plainTextEdit->toPlainText().split("\n");
i=x.size();
double y[i],x_medio=0.0,sigma2=0.0,sigma;
for (j=0;j<i;j++){
y[j]=x.at(j).toDouble();
}
for (j=0;j<i;j++)
{
x_medio=y[j]/i+x_medio;
}
for(j=0;j<i;j++)
{
sigma2=sigma2+pow(y[j]-x_medio,2.0)/(i*(i-1.0));

}
sigma=sqrt(sigma2);
ui->label->setNum(x_medio);
ui->label_2->setNum(sigma);
ui->plainTextEdit->clear();
}

Ginsengelf
7th October 2013, 07:12
Hi, I think the easier and more standard way would be to use a QDoubleSpinBox instead of a restricted text edit field.

Ginsengelf