Qt QProcess stay_open example

Started by Rory, February 15, 2021, 02:44:04 PM

Previous topic - Next topic

Rory

It took me a little longer than it should have to develop this code to run ExifTool in stay_open mode within the Qt environment.  Any suggestions for further improvement are most welcome.

This function copies all the metadata from the source file list to the destination file list.


int ExifTool::copyAll(const QStringList &src, QStringList &dst)
{
    // initial arguments to keep ExifTool open
    QStringList stayOpen;
    stayOpen << "-stay_open";
    stayOpen << "True";
    stayOpen << "-@";
    stayOpen << "-";

    // stdin stream with copy tags for each pair of files in the lists
    QByteArray args;
    for (int i = 0; i < src.length(); i++) {
        args += "-TagsFromFile\n";
        args += src.at(i) + "\n";       // src = path of source file
        args += "-all:all\n";
        args += dst.at(i) + "\n";       // dst = path of destination file
        args += "-execute\n";
    }
    // terminate stay_open
    args += "-stay_open\n";
    args += "False\n";

    QProcess et;
    et.start(exifToolPath, stayOpen);   // exifToolPath = path to ExifTool executable
    et.waitForStarted(3000);
    // write to stdin
    et.write(args);
    if (et.waitForFinished(30000)) { Put your success code here }
    else { Put your failure code here }
   
    return et.exitCode();               // return ExifTool exit code
}

Phil Harvey

You don't actually have to use -stay_open here since you are doing this synchronously and stopping the exiftool process afterward.  You could just as well do it more simply like this (but I can't test it out):

int ExifTool::copyAll(const QStringList &src, QStringList &dst)
{
    // stdin stream with copy tags for each pair of files in the lists
    QStringList args;
    for (int i = 0; i < src.length(); i++) {
        args << "-TagsFromFile";
        args << src.at(i);       // src = path of source file
        args << "-all:all";
        args << dst.at(i);       // dst = path of destination file
        args << "-execute";
    }

    QProcess et;
    et.start(exifToolPath, args);       // exifToolPath = path to ExifTool executable
    return et.exitCode();               // return ExifTool exit code
}


- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Rory

Yes, that makes total sense. Thanks Phil.