Close program correctly



  • I have a code

    In header file
    @Eng* dialog;@

    In MainWindow
    @void MainWindow::on_pushButton_clicked()
    {
    if( w.size() < 3)
    {
    QMessageBox box;
    box.setText(QString("Error: Count of words less then 3"));
    box.exec();
    exit(1);
    }
    dialog->show();
    dialog->start(w,0);
    }@

    And in eng.cpp file
    @void Eng::start(Words w, int type)
    {
    word = w;
    size = w.size();
    ui->label3->setNum(size);
    int error = 0;
    int all = 0;
    while(true)
    {
    ui->pushButton1->setStyleSheet("");
    ui->pushButton2->setStyleSheet("");
    ui->pushButton3->setStyleSheet("");

        setAs();
        right = rand() % 3;
    
        speak(w.getEng( a[right] ));
    
        ui->label1->setText("&lt;html&gt;&lt;head/>&lt;body&gt;&lt;p><span style=\"font-size:26pt;\">" + (type == 0 ? w.getRus( a[right] ) : w.getEng( a[right] )) + "</span></p>&lt;/body&gt;&lt;/html>" );
    
        ui->pushButton1->setText(type == 0 ? w.getEng( a[0] ) : w.getRus( a[0] ));
        ui->pushButton2->setText(type == 0 ? w.getEng( a[1] ) : w.getRus( a[1] ));
        ui->pushButton3->setText(type == 0 ? w.getEng( a[2] ) : w.getRus( a[2] ));
    
        QEventLoop loop,loopNext;
        QObject::connect(this,SIGNAL(clickedButton()),&loop,SLOT(quit()));
        loop.exec&#40;&#41;;
    
        if(right == 0) ui->pushButton1->setStyleSheet("background-color: #40DE06");
        else if(right == 1) ui->pushButton2->setStyleSheet("background-color: #40DE06");
        else ui->pushButton3->setStyleSheet("background-color: #40DE06");
    
        all++;
        if(right != choice)
        {
            if(choice == 0) ui->pushButton1->setStyleSheet("background-color: #E80D0D");
            else if(choice == 1) ui->pushButton2->setStyleSheet("background-color: #E80D0D");
            else ui->pushButton3->setStyleSheet("background-color: #E80D0D");
            error++;
        }
    
        double percentError = ((int)(((double)error/all)*10000)) / 100.0;
        ui->label5->setText(QString::number(percentError) + "%" + " (" + QString::number(error) + ")");
        ui->label5->setStyleSheet("color: #AA0000");
    
        QObject::connect(ui->buttonNext,SIGNAL(released()),&loopNext,SLOT(quit()));
        loopNext.exec&#40;&#41;;
    }
    

    }@

    When i close dialog and after close mainwindow, i get infinite cycle (program not closes!). I wanna click on red X in right upper corner of window and close program



  • use @qApp->exit(0);@ in closeEvent



  • Yeah! it's works. thx u


  • Lifetime Qt Champion

    Hi,

    This is one solution to do it but you should maybe consider a redesign of that part of the code. Using an infinite loop like this is not really recommended when doing a UI especially since you don't offer any possibility to get out of it.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.