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. QProcess and dd under linux
Forum Updated to NodeBB v4.3 + New Features

QProcess and dd under linux

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 2.7k Views 1 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.
  • S Offline
    S Offline
    stephane78
    wrote on last edited by
    #1

    Hi,
    I am trying without success to launch dd to copy one file with QProcess under linux.I want to wait that my dd command is finished.I have tried without successss with QProcess::execute and with Qprocess::startdetached (static functions). I don't know if I need a "/bin/sh -c" before my dd command or if I don't need "/bin/sh -c".the copy with dd doesn't happen.(This code that call QProcess and dd is in a secondary thread).I have tried with and without "/bin/sh -c" before dd.my dd command is like "dd if=source file of=destination file".

    example code without /bin/sh -c :

    QString cmd;
    int res2;
    
    cmd=QString("dd if=")+strFileIn+QString(" of=")+strFileOut;
    res2=QProcess::execute(cmd);
    

    or
    with /bin/sh -c :

    qint64 pid;
            QString arg0("-c");
    
    	QString arg1("if=");
    	arg1+=strFileIn;
            QString arg2(" of=");
    	arg2+=strFileOut;
            arg2+="\"";
            QString arg3("\"dd ");
            arg3+=arg1+arg2;
    	QStringList list;
            list+=arg0;
            list+=arg3;
            //list+=arg2;
    	int status;
            bool res3=QProcess::startDetached("/bin/sh",list,QString(),&pid);
    	
    	
            waitpid(pid,&status,0);
    

    Has someone any good idea ?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      You need sh -c if external command/exe file - afaik
      you should check with "man sh"

      There is waitForFinished to wait for completion.

      Try the ping example

      QProcess pingProcess;
      QString exec = "ping";
      QStringList params;
      params << "-c" << "1" << IP;
      pingProcess.start(exec,params,QIODevice::ReadOnly);
      pingProcess.waitForFinished(-1);
      QString p_stdout = pingProcess.readAllStandardOutput();
      QString p_stderr = pingProcess.readAllStandardError();
      qDebug() << p_stdout ;
      qDebug() << p_stderr ;

      1 Reply Last reply
      0
      • S Offline
        S Offline
        stephane78
        wrote on last edited by
        #3

        @mrjj Hi,
        Ok I have debugged to find the problem and I have this error :

        Object: Cannot create children for a parent that is in a different thread.
        (Parent is TaskThread(0x1c08840), parent's thread is QThread(0x14449a0), current thread is TaskThread(0x1c08840)

        the function that call this QProcess code is in the secondary thread "TaskThread". It seems it is not possible to call a method of QProcess like start or execute in this secondary thread (I am with Qt 5.6.0 under linux)
        do you have any further idea ?

        jsulmJ 1 Reply Last reply
        0
        • S stephane78

          @mrjj Hi,
          Ok I have debugged to find the problem and I have this error :

          Object: Cannot create children for a parent that is in a different thread.
          (Parent is TaskThread(0x1c08840), parent's thread is QThread(0x14449a0), current thread is TaskThread(0x1c08840)

          the function that call this QProcess code is in the secondary thread "TaskThread". It seems it is not possible to call a method of QProcess like start or execute in this secondary thread (I am with Qt 5.6.0 under linux)
          do you have any further idea ?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @stephane78 You should show how you start that second thread and what exactly you're doing there.
          Is there a reason why you use a second thread?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            Hi
            well it must be done differently, then it should work

            http://stackoverflow.com/questions/3268073/qobject-cannot-create-children-for-a-parent-that-is-in-a-different-thread

            https://wiki.qt.io/Threads_Events_QObjects

            So maybe you just need to send a signal to thread and slot will start QProc.

            1 Reply Last reply
            1
            • S Offline
              S Offline
              stephane78
              wrote on last edited by
              #6

              @mrjj,jsulm,Hi again, I have a solution with system but as I have corrected two other errors I will try again with QProcess so I am not sure that the secondary thread was the error.so my first error : I had forgotten to create the destination path of the destination file (the subdirectory) so it made an error and the second error: I should have removed the character '\' of the source and destination file strings and put " before and after the two string of file (if="source" and of="destination".
              with these two errors corrected it runs well with system(cmd) and I will try again now with QProcess and come back to report here if it works...

              1 Reply Last reply
              0
              • S Offline
                S Offline
                stephane78
                wrote on last edited by
                #7

                so it works too with QProcess::execute(cmd).
                As I said previously I removed the character '\' that is in the strings source and destination before the spaces.I must still think about the accentued characters in the source and destination
                files.

                but my problem is solved....

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved