vendredi 4 septembre 2015

Qt dialog accept and reject

I'm trying to implement the following thing: when a specific button is clicked, a new dialog appears where the user has to enter some information and to check/uncheck some checkboxes. Then the user can either click "OK" or "Cancel".

If he clicks "OK" the information he entered is checked for correctness. If something is not correct the dialog should appear again with a warning message and the user should correct/re-enter the information. I would like the information the user entered to be stored. So, if the information is not correct, the dialog should not "reset" to the initial state but keep the information the user entered.

If the user clicks "Cancel" some standard values are used further.

I almost got a solution, but it is not working properly. When using my solution: when I enter a wrong information and click "OK" a warning appears and the information is stored and I can edit it. But if I enter wrong information again and click "OK" again, then the wrong information is accepted. Please see my code below.

QDialog dialog(this);
QFormLayout form(&dialog);

form.addRow((new QLabel("Please enter the three questions for the P835 test. \n"
                        "Questions one and two will be permuted, \n "
                        "question three will not. Below each question enter \n"
                        "the rating scale starting with the best rating and \n"
                        "separate the ratings with a comma.")));
QList<QLineEdit *> fields;

    QLineEdit *lineEdit_Q1 = new QLineEdit(&dialog);
    lineEdit_Q1->setText("Bitte bewerten Sie die Signalqualität!");
    QString label_Q1 = QString("First question:");
    form.addRow(label_Q1, lineEdit_Q1);
    fields << lineEdit_Q1;

    QLineEdit *lineEdit_Q1_answer = new QLineEdit(&dialog);
    lineEdit_Q1_answer->setText("nicht verzerrt, leicht verzerrt, etwas verzerrt, ziemlich verzerrt, sehr verzerrt");
    QString label_Q1_answer = QString("Rating first question:");
    form.addRow(label_Q1_answer, lineEdit_Q1_answer);
    fields << lineEdit_Q1_answer;

QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &dialog);
    form.addRow(&buttonBox);
    QObject::connect(&buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()));
    QObject::connect(&buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));
    bool click = true;
    int code = dialog.exec();
    bool passed = true;
    while (click == true){
        passed = true;
        if (code == QDialog::Accepted) {
        // check if empty questions were entered
        if (lineEdit_Q1->text() == "" || lineEdit_Q2->text() == "" || lineEdit_Q3->text() == "") {
            QMessageBox msgBox;
            msgBox.setText("An error occured while entering questions for the P835 test");
            msgBox.setInformativeText("You can not enter empty questions! Please try again or click cancel to use the standard questions!");
            msgBox.setIcon(QMessageBox::Warning);
            msgBox.exec();
            passed = false;
            dialog.close();
            dialog.exec();
            break;
        }
        if (lineEdit_Q1_answer->text().split(",").size() != 5 || lineEdit_Q2_answer->text().split(",").size() != 5 || lineEdit_Q3_answer->text().split(",").size() != 5) {
            QMessageBox msgBox;
            msgBox.setText("An error occured while entering question ratings for the P835 test");
            msgBox.setInformativeText("You have to enter exactly 5 ratings for each question! Please try again or click cancel to use the standard ratings!");
            msgBox.setIcon(QMessageBox::Warning);
            msgBox.exec();
            passed = false;
            dialog.close();
            dialog.exec();
            break;
        }
        if (oneFileCheckBox->isChecked() && multipleFilesCheckBox->isChecked()) {
            QMessageBox msgBox;
            msgBox.setText("An error occured while setting up the P835 test...");
            msgBox.setInformativeText("You cannot check both boxes! Please select only one option for the files!");
            msgBox.setIcon(QMessageBox::Warning);
            msgBox.exec();
            passed = false;
            dialog.close();
            dialog.exec();
            break;
        }
        if (oneFileCheckBox->isChecked() == false && multipleFilesCheckBox->isChecked() == false) {
            QMessageBox msgBox;
            msgBox.setText("An error occured while setting up the P835 test...");
            msgBox.setInformativeText("You have to select one file option!");
            msgBox.setIcon(QMessageBox::Warning);
            msgBox.exec();
            passed = false;
            dialog.close();
            dialog.exec();
            break;
        }
        if (passed == true) {
            this->configMgr->setQuestions(lineEdit_Q1->text(), lineEdit_Q2->text(), lineEdit_Q3->text());
            this->configMgr->setAnswers(lineEdit_Q1_answer->text(), lineEdit_Q2_answer->text(), lineEdit_Q3_answer->text());
            if(oneFileCheckBox->isChecked() == true) {
                this->configMgr->fileOption = 0;
            }
            if(multipleFilesCheckBox->isChecked() == true) {
                this->configMgr->fileOption = 1;
            }
            QMessageBox msgBox;
            msgBox.setText("Success!");
            msgBox.setInformativeText("The questions and the question ratings have been set successfully!");
            msgBox.setIcon(QMessageBox::Information);
            msgBox.exec();
            dialog.close();
            click = false;
        }
        if (code == QDialog::Rejected) {
            this->configMgr->setQuestions(Q1_std, Q2_std, Q3_std);
            this->configMgr->setAnswers(Q1_std_answer, Q2_std_answer, Q3_std_answer);
            QMessageBox msgBox;
            msgBox.setText("Setting standard values...");
            msgBox.setInformativeText("Standard questions and ratings will be set. Click on the P835 button again to set questions and ratings manually!");
            msgBox.setIcon(QMessageBox::Information);
            msgBox.exec();
            dialog.close();
            click = false;
        }
        }
        if (code == QDialog::Rejected) {
            this->configMgr->setQuestions(Q1_std, Q2_std, Q3_std);
            this->configMgr->setAnswers(Q1_std_answer, Q2_std_answer, Q3_std_answer);
            QMessageBox msgBox;
            msgBox.setText("Setting standard values...");
            msgBox.setInformativeText("Standard questions and ratings will be set. Click on the P835 button again to set questions and ratings manually!");
            msgBox.setIcon(QMessageBox::Information);
            msgBox.exec();
            dialog.close();
            click = false;
        }
    }

In this example code I only put one text box where the user has to enter some information. I have six text boxes and two checkboxes.

I hope you can help me! Thank you!




Aucun commentaire:

Enregistrer un commentaire