dimanche 25 novembre 2018

QLineEdit and QComboBox have unrelated behavior

I have a user interface with a lot of controls. However I have a problem with a QLineEdit and a QComboBox that are not responding properly. I am basically converting from pixel measurements to millimeters/centimeters/decimeters and meters with a QComboBox and showing the result on a QLineEdit. For the conversion table I used this page. When I choose fromPixelToMillimeters() it does the conversion, but when I choose fromPixelToCentimeters() I think it is using the present value after the first conversion of fromPixelToMillimeters(). And if I go back choosing fromPixelToMillimeters() I get a different result too. This happens continuously, I get different measures each time.

See the code below:

void MainWindow::on_cBoxMeasures_currentIndexChanged(const QString &arg1)
{
    if(arg1 == "Select Conversion(s)") {
        return ui->leftLineEditDist->setText(QString("%1").arg(ui->leftLineEditDist->text().toDouble()));
    } else if(arg1 == "pixel") {
        return ui->leftLineEditDist->setText(QString("%1").arg(ui->leftLineEditDist->text().toDouble()));
    } else if(arg1 == "mm") {
        return fromPixelToMillimeters();
    } else if(arg1 == "dm") {
        return fromPixelToDecimeters();
    } else if(arg1 == "cm") {
        return fromPixelToCentimeters();
    } else if(arg1 == "m") {
        return fromPixelToMeters();
    }
}

void MainWindow::fromPixelToMillimeters()
{
    double mm = ui->leftLineEditDist->text().toDouble();
    double dpi = 300;
    double totalDistanceInMillimeter = (mm*25.4)/dpi;
    ui->leftLineEditDist->setText(QString("%1").arg(totalDistanceInMillimeter));
    ui->leftLineEditDist->show();
}

void MainWindow::fromPixelToCentimeters()
{
    double mm = ui->leftLineEditDist->text().toDouble();
    double dpi = 300;
    double totalDistanceInCm = ((mm*25.4)/dpi)*0.1;
    ui->leftLineEditDist->setText(QString("%1").arg(totalDistanceInCm));
    ui->leftLineEditDist->show();
}

void MainWindow::fromPixelToDecimeters()
{
    double mm = ui->leftLineEditDist->text().toDouble();
    double dpi = 300;
    double totalDistanceInDcm = ((mm*25.4)/dpi)*0.01;
    ui->leftLineEditDist->setText(QString("%1").arg(totalDistanceInDcm));
    ui->leftLineEditDist->show();
}

void MainWindow::fromPixelToMeters()
{
    double mm = ui->leftLineEditDist->text().toDouble();
    double dpi = 300;
    double totalDistanceInM = ((mm*25.4)/dpi)*0.001;
    ui->leftLineEditDist->setText(QString("%1").arg(totalDistanceInM));
    ui->leftLineEditDist->show();
}

void MainWindow::on_cBoxMeasures_currentIndexChanged(int index)
{
    switch (index) {
    case(0):
        break;
    case(1):
        break;
    case(2):
        fromPixelToMillimeters();
        break;
    case(3):
        fromPixelToCentimeters();
        break;
    case(4):
        fromPixelToDecimeters();
        break;
    case(5):
        fromPixelToMeters();
        break;
    }
}

Please advise on what the problem might be.

Aucun commentaire:

Enregistrer un commentaire