Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Close program correctly
Qt 6.11 is out! See what's new in the release blog

Close program correctly

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bagipro
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mehrdadsilver
      wrote on last edited by
      #2

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

      Mehrdad Abdolghafari, Be silver

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bagipro
        wrote on last edited by
        #3

        Yeah! it's works. thx u

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved