1
votes

I am just trying to create a file with QProcess by the following source code:

void Processmethod()
{

    QDialog *ProcessMessage = new QDialog;      
    Ui::DialogProcessMessage Dialog;            
    Dialog.setupUi(ProcessMessage);             
    ProcessMessage->setModal(true);
    ProcessMessage->setAttribute(Qt::WA_DeleteOnClose); 
    ProcessMessage->show();

    processmethodONE(); 
} 

void processmethodONE()
{
    QString ProcessCommand = "w8 " + blablubli";            

    Prozess.setWorkingDirectory(Path);         //QProcess "Prozess" is globaly defined  
    Prozess.setStandardOutputFile(Path);       //in my class
    Prozess.start(ProcessCommand);

    QProcess::ExitStatus Status = Prozess.exitStatus(); 

    if (Status == 0)
    {
        std::cout << "File created!" << std::endl;
    }
}

This process creates out of another file which is located in the QString "Path" a new file, let me call it "PROFILE" (PRO for Problem :). The Profile also is created, but is never completed, I guess not even 50% of the file are completed. Just when using

while(!Prozess.waitForFinished())
      {
       qApp->processEvents();
       std::cerr << "Process Create PROFile running " << std::endl;
      }

the file is written completely. Furthermore I tried to use the QProcess finished(int) Signal to start another method and deleted the while loop (which freezes the whole GUI). I declared it in the constructor with:

connect(&Prozess, SIGNAL(finished(int)), this, (SLOT(processmethodTWO())));

But I guess this could not work because the first process isn't finished completely. Where is the problem?

1
"great" comment dude. would be nice if you just could tell me what you mean by that? This is a forum! :P - Streight
call me pedantic and silly, but 1) giving functions a name with "function" or "method" in them is daft (although for a pure example this helps to convey your message, so just don't do it in real life code) and 2) "method" is a Java-ism. Nowhere in the C or C++ standard is that term used, and it makes me shudder to think that a Java programmer is trying to learn C++. Nothing personal ;-) - rubenvb
1) Yeah, I did that for the example, since I am no real professional programmer I just did that to make it more clear, but obviously every good programmer will see that this is a method on first view :). 2) Yeah right, I started with Java and therefore had the name "method" in mind, but when I am right I also have seen/read some german c++ tutorials where they used the name "methods" and told that "function" is more a mathematic notation (like intz = x+y). ;) - Streight

1 Answers

2
votes

There is no warranty that right after Prozess.start(ProcessCommand); process will be finished, so calling "Prozess.exitStatus();" right after it will give you "false positive". You certainly will have to wait until process is finished. You may either do it with with while loop, that you suggested or with QEventLoop

// ...
Prozess.setStandardOutputFile(Path);       //in my class
QEventLoop loop;
connect(&Prozess, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit()));
connect(&Prozess, SIGNAL(finished(int, QProcess::ExitStatus)), &loop, SLOT(quit()));
Prozess.start();
loop.exec();
// Now your process status should be valid:
QProcess::ExitStatus Status = Prozess.exitStatus();