Qt and fork()



  • 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!");
    }
    

    }@


  • Lifetime Qt Champion

    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.



  • 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();
    }
    @


  • Lifetime Qt Champion

    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.


Log in to reply
 

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