Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Qt and fork()

    QML and Qt Quick
    2
    4
    7401
    Loading More Posts
    • 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.
    • T
      trolinio last edited by

      I want to write some wrapper to console app. The problem is I cant understand why label_2 isn't changed to "Wait" till converter is done working. I think the main process should change it's text to "Wait" and then split by fork(), wait till converted is done and then change labet's text to "done".

      @Form::Form(QWidget* parent){
      setupUi(this);
      connect(pushButton, SIGNAL(clicked()), this, SLOT(generate()));

      this->tmpFilename = "word_list.txt";
      

      }

      void Form::generate(){
      label_2->setText("Wait!");
      pid_t PID = fork();

      if(PID == 0){
          QFile file(this->tmpFilename);
          file.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text);
          QTextStream fileStream(&file);
          fileStream <&lt; plainTextEdit-&gt;toPlainText();
          file.close();
          
          execl("./converter", "converter", this->tmpFilename, NULL);
          exit(0);
          
      } else if(PID > 0){
          wait(NULL);
          label_2->setText("Done!");
      }
      

      }@

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi and welcome to DevNet,

        If I understand your code correctly you are trying to start a process by forking your current application and if I'm not wrong it's not how fork should be used (IIRC it's rather used in CLI application). But since you are using Qt, I'd recommend QProcess to achieve what you want.

        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 Reply Quote 0
        • T
          trolinio last edited by

          Same problem with QProcess. Program works but there is no "Wait" information shown. After I click button program does it's job well and after a while there's just "Done".

          @#include <QtGui>
          #include "Form.h"

          Form::Form(QWidget* parent){
          setupUi(this);
          connect(pushButton, SIGNAL(clicked()), this, SLOT(generate()));

          converterProc = new QProcess(this);
          connect(converterProc, SIGNAL(started()), this, SLOT(converterStarted()));
          connect(converterProc, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(converterFinished()));
          

          }

          void Form::generate(){
          QFile file("word_list.txt");
          file.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text);
          QTextStream fileStream(&file);
          fileStream << plainTextEdit->toPlainText();
          file.close();

          converterProc->start("./converter word_list.txt");
          converterProc->waitForFinished();
          

          }

          void Form::converterStarted(){
          label_2->setText("Wait!");
          label_2->repaint();
          }

          void Form::converterFinished(){
          label_2->setText("Done!");
          label_2->repaint();
          }
          @

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            waitForFinished blocks the event loop so converterStarted and converterFinished will be called one after the other after converterProc has ended thus you won't see "Wait!".

            If you just want to avoid several start of the converter and keep you UI responsive, you could disable pushButton while the converter is running.

            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 Reply Quote 0
            • First post
              Last post