Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Qt and fork()

Qt and fork()

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 7.9k Views
  • 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 Offline
    T Offline
    trolinio
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • T Offline
        T Offline
        trolinio
        wrote on last edited by
        #3

        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
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          0

          • Login

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