Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Reusing the same QProcess variable in a loop



  • Is it possible to reuse the same QProcess variable more than once? The reason why I ask is because I would like to unmount several partitions from an external hard drive. Currently, if a USB is connected I am able to unmount it. However, if I have an external hard drive with more than one partition, it only unmounts the first partition it founds.

    /* The command to check the USBs disk location is lsblk | grep USBNAME | awk '{print $1 }'
     * However, because this is a chain command, it is divided into 3 commands
     */
    QProcess command1; //lsblk - all USB's information
    QProcess command2; //grep USBname - info on specific USB
    QProcess command3; //awk '{print $1 }' - grab just the USB name
    QProcess command4; //unmount and detach USBs
    
    QString disk;
    
    //chain command
    command1.setStandardOutputProcess(&command2);
    command2.setStandardOutputProcess(&command3);
    command3.setProcessChannelMode(QProcess::MergedChannels);
    
    //get the disk names of all USBs connected
    for(int j = 0; j < _USBlist.size(); j++)
    {
        QStringList arg = QStringList()<<_USBlist[j];
        QStringList arg1 = QStringList()<<"{print $1 }";
    
        command1.start("lsblk");
        command2.start("grep",arg);
        command3.start("awk", arg1);
    
        command3.waitForFinished();
        QByteArray output = command3.readAll();
    
        QString outputTemp = QString(output);
        QString diskName = ""; //get diskname
        disk = "";  //get disk
    
        for(int i=0; i< outputTemp.size(); i++)
        {
            if(outputTemp.at(i).isLetter())
            {
                disk = disk + outputTemp.at(i);
                diskName = diskName + outputTemp.at(i);
            }
            else if(outputTemp.at(i).isNumber())
            {
            diskName = diskName + outputTemp.at(i);
            }
        }
        QStringList arg2 = QStringList()<<"--unmount" << "/dev/" + diskName;
        command4.start("udisks", arg2);
        command4.waitForFinished();
    
        arg.clear();
        arg1.clear();
        arg2.clear();
    
    }
    QStringList arg3 = QStringList()<<"--detach" << "/dev/" + disk;
    command4.start("udisks", arg3);
    command4.waitForFinished();
    
    arg3.clear();
    command1.close();
    command2.close();
    command3.close();
    command4.close();


  • have you checked error(), errorString(), exitCode(), exitStatus() ?

    I've run into somewhat similar problem.
    In my program process runs fine the first time, but after that it keeps giving me "No such file or directory" and ProcessError::FailedToStart



  • @thebeast444 All I get is

    "Unknown error"
    QProcess::start: Process is already running


  • Lifetime Qt Champion

    Hi,

    Maybe QEasyShell would make the whole process easier.



  • Now QEasyShell sounds kewl :) I might find a place or two for something like that :)



  • I realized that the command was wrong and wasn't recognizing my partitions.


Log in to reply