Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Solved Reusing the same QProcess variable in a loop

    General and Desktop
    qprocess
    4
    6
    2136
    Loading More Posts
    • 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
      marlenet15 last edited by

      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 Reply Quote 0
      • T
        thebeast444 last edited by

        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 Reply Quote 0
        • M
          marlenet15 @thebeast444 last edited by

          @thebeast444 All I get is

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

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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 Reply Quote 1
            • L
              Lineaxe last edited by Lineaxe

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

              1 Reply Last reply Reply Quote 0
              • M
                marlenet15 last edited by

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

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post