Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Reusing the same QProcess variable in a loop
Forum Updated to NodeBB v4.3 + New Features

Reusing the same QProcess variable in a loop

Scheduled Pinned Locked Moved Solved General and Desktop
qprocess
6 Posts 4 Posters 2.7k Views 2 Watching
  • 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.
  • M Offline
    M Offline
    marlenet15
    wrote on last edited by
    #1

    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();
    
    1 Reply Last reply
    0
    • T Offline
      T Offline
      thebeast444
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • T thebeast444

        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

        M Offline
        M Offline
        marlenet15
        wrote on last edited by
        #3

        @thebeast444 All I get is

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Maybe QEasyShell would make the whole process easier.

          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
          1
          • L Offline
            L Offline
            Lineaxe
            wrote on last edited by Lineaxe
            #5

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

            1 Reply Last reply
            0
            • M Offline
              M Offline
              marlenet15
              wrote on last edited by
              #6

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

              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