Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Code Block

\#include "dialog.h"

\#include "ui_dialog.h"

\#include <QMessageBox>

\#include <QDoubleValidator>

Dialog::Dialog(QWidget \*parent)

: QDialog(parent), ui(new Ui::Jakolaskin)

{

  ui->setupUi(this);

  ui->pushButton->setEnabled(false);

  ui->lineEdit->setValidator(new QDoubleValidator(this));

  ui->lineEdit_2->setValidator(new QDoubleValidator(this));

  connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(onLaske()));

  connect(ui->lineEdit,SIGNAL(textChanged(const QString &)),this,SLOT(onTextChanged(const QString &)));

  connect(ui->lineEdit_2,SIGNAL(textChanged(const QString &)),this,SLOT(onTextChanged(const QString &)));

}

Dialog::~Dialog()

{

  delete ui;

}

void Dialog::onLaske()

{

  QMessageBox box;

  bool bOK1, bOK2;

  double value1 = ui->lineEdit->text().toDouble(&bOK1);

  double value2 = ui->lineEdit_2->text().toDouble(&bOK2);

  ui->lineEdit_3->setText("");

  if(bOK1 && bOK2)

  {

    if(value2 \!= 0)

    {

      double result = value1 / value2;

      ui->lineEdit_3->setText(QString::number(result));

    }

    else

    {

      box.setText("Et voi jakaa nollalla");

      box.exec();

    }

  }

  else

  {

    box.setText("Virheellinen syöte");

    box.exec();

  }

}

void Dialog::onTextChanged(const QString &)
{
  ui->pushButton->setEnabled(ui->lineEdit->text() \!= "" &&  ui->lineEdit_2->text() \!= "");
}