Skip to content
  • 0 Votes
    8 Posts
    3k Views
    JonBJ

    @BitFlipper
    b) I tried that already with no luck

    Then you did not try right! There is an example of just what you need at the link I posted. The outline of this approach would be:

    QProcess ddProcess, md5sumProcess; ddProcess.setStandardOutputProcess(&md5sumProcess); ddProcess.start("sudo dd if=/dev/sda2 of=/dev/sdb2 bs=4096 conv=noerror"); md5sumProcess.start("md5sum"); md5sumProcess.waitForFinished();

    However, it's probably more suitable for you to issue the whole lot as a single string passed to /bin/sh or /bin/bash with the -c argument, and let it figure the | for you., as per my example earlier.

    So my command changes in this function, should I build a custom string every time and pass it in as one statement?

    Yes.

    I always practice in terminal first

    For this purpose make yourself use /bin/sh -c "sudo dd ... | md5sum" as that is what you will need. Be careful if anything in your command requires its own quoting (your current example does not), as the whole command itself is now inside quotes.

    As I wrote earlier, your md5sum is not going to see the contents from the dd. Your example does not lend itself to checksumming as it uses a single dd if=... of=... which does the input & output in one go. What you want is for the output from the dd to go both to the output file and to md5sum. Here are two possibilities for you to play with:

    sudo dd if=/dev/sda2 bs=4096 conv=noerror | sudo tee /dev/sdb2 | md5sum sudo dd if=/dev/sda2 bs=4096 conv=noerror | tee >(md5sum 1>&2) | sudo dd of=/dev/sdb2 bs=4096

    In the first case we tee the output off to /dev/sdb2 as well as letting it through to md5sum. Simpl-ish, but you lose the ability to specify the bs= for the output to /dev/sdb2. I don't know if that matters to you.

    In the second case you use "shell magic" (you'll probably have to use /bin/bash not /bin/sh, I think) to send tee's output to md5sum process as well as passing it onto a second dd to do the output. I have made it so md5sum's output goes to standard error instead of standard output.

    Finally: is all this dd and checksum stuff worth it? Probably not. Have a read of, say, https://unix.stackexchange.com/a/45854/104736 for alternative suggestions.

  • 0 Votes
    3 Posts
    861 Views
    A

    Hi,

    I have found something that works but that is not really "clean" in my opinion :

    QString password = "yourRootPassword"; //could be asked with QInputDialog::getText(...) QString cmd = QString("sudo -S kextunload -b %1 > /dev/null").arg(driverName); FILE *pipe = popen(cmd.toStdString().c_str(), "w"); if(pipe != nullptr) { fprintf(pipe, "%s\n", password.toStdString().c_str()); if (ferror(pipe)) { qDebug() << "Failed to write to pipe"; } else { qDebug() << "Written to pipe"; } } else { qDebug() << "Failed to open pipe"; } qDebug() << "Pipe returned : " << pclose(pipe);

    I don't know how to use the Apple method linked by SGaist...

  • 0 Votes
    4 Posts
    983 Views
    QjayQ

    thanks !! . I will look into both of them

  • 0 Votes
    10 Posts
    2k Views
    SGaistS

    You might have modified PATH in your terminal session only for example.

    Or you may have modified PATH in the Run part of the Project panel.

  • 0 Votes
    10 Posts
    7k Views
    SGaistS

    Hi,

    There is a simpler method: setStandardOutputProcess.

    Use one QProcess for each element of your pipe. That way you recreate your chain in code and no dependency on shell handling.

  • 0 Votes
    2 Posts
    860 Views
    SGaistS

    Hi and welcome to devnet,

    You should take a look at the various IPC options provided by Qt.

  • 0 Votes
    22 Posts
    5k Views
    SGaistS

    IIRC, you have QTest;:wait for that kind of stuff.

  • 0 Votes
    13 Posts
    4k Views
    jsulmJ

    @Naim Use http://doc.qt.io/qt-5/qprocess.html#readyReadStandardOutput and http://doc.qt.io/qt-5/qprocess.html#readyReadStandardError to get notifications each time the process writes something to stdout or stderr and read it using http://doc.qt.io/qt-5/qprocess.html#readAllStandardOutput and http://doc.qt.io/qt-5/qprocess.html#readAllStandardError. Do not call waitForFinished, just make sure your proc does not go out of scope (make it class member).

  • 0 Votes
    2 Posts
    1k Views
    JonBJ

    @gizalp
    Why do you think python (or anything else) is going to open any terminal window? And what OS are you on anyway?

    For QProcess:setProgram you should have process.setProgram("python");, only. But in this case I think it's getting overridden by your process.start("python test.py"); so it doesn't matter, but it's wrong.

    If you want to grab a sub-process's output, you have to handle QProcess::readyReadStandardOutput(), and in there something like QByteArray QProcess::readAllStandardOutput(), if that's what you mean by "How can I get the 'Hello' output?".

    How this relates to Trying to implement thumbnail list I have no idea....

  • Qt Signal Slot

    Solved General and Desktop
    6
    0 Votes
    6 Posts
    3k Views
    M

    @gizalp said in Qt Signal Slot:

    @mostefa Thanks, that fixed my problem!

    You are welcome =)

  • 0 Votes
    7 Posts
    4k Views
    A

    @kshegunov I see, now i understand. Thank you for your help and for your patience.

  • 0 Votes
    2 Posts
    763 Views
    VRoninV

    You can't read from a finished program. you have to connect to readyRead();

    replace

    process->start(cmd,comandlist,QIODevice::ReadOnly ); if (!process->waitForFinished()) { lstr = QString(); } else { lstr = QString(process->readAll().constData()); } return lstr;

    with

    QObject::connect(process,&QProcess::readyReadStandardOutput,[&lstr,process]()->void{lstr.append(QString::fromLatin1(process->readAllStandardOutput()));}); process->start(cmd,comandlist,QIODevice::ReadOnly ); if (process->waitForFinished()) return lstr; return QString();
  • 0 Votes
    13 Posts
    28k Views
    J

    @SGaist Good one! Though I had to use the C++11 version since C++14 is still in Debian Sid.

    // C++11 connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &MainWindow::close);

    For those who want to know, this is the C++14-version:

    // C++14 connect(process, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), this, &MainWindow::close);

    Since the actual problem of this thread is solved, I will mark this thread as solved tomorrow. In the mean time comments on the above code can be made.

  • 0 Votes
    7 Posts
    5k Views
    J

    Well, actually it does work! But if bash is not running interactively, it won't read the .bashrc where the shell-variables are stored. So I did the following:

    sh.start("bash -i"); // the -i tell bash to run interactively if(sh.waitForStarted()) sh.write("printenv JAVA_HOME"); // or sh.write("echo $JAVA_HOME") sh.closeWriteChannel();

    In case you were wondering, I connected the QProcess::finished signal to a slot where the QProcess::readAllStandardOutput is put in a QPlainTextEdit.

    The QProcess::processEnvironment is not updated however. Even if I try to export JAVA_HOME with sh.write("export JAVA_HOME"). While that is not what I expected, I now can find the local shell-variables.

    So I will mark this problem as solved.
    Thanks!

  • 0 Votes
    2 Posts
    892 Views
    dheerendraD

    Can you share the QProcess code you have written to execute the script ?

  • 0 Votes
    2 Posts
    5k Views
    jsulmJ

    @CybeX You already asked more or less the same question in another thread. In that another thread you was tolled to use the assynchronous API (there was even code for you). You should not try to do it in synchronous way, you are working against Qt. Use signals and slots.
    https://forum.qt.io/topic/75454/qprocess-readall-and-qprocess-readallstandardoutput-both-return-an-empty-string-after-qprocess-write-is-run/6

  • 0 Votes
    15 Posts
    18k Views
    B

    QProcess has quite extensive support for synchronous use.

    #include <QCoreApplication> #include <QDebug> #include <QProcess> int main(int argc, char **argv) { QCoreApplication app(argc, argv); QProcess process; process.start("ls", {"-l"}); if (process.waitForFinished()) { qDebug() << "returned:" << process.readAllStandardOutput(); } else { qDebug() << process.errorString(); } process.start("ls", {"-l"}); while (process.waitForReadyRead()) { while (process.canReadLine()) qDebug() << process.readLine().trimmed(); } }
  • 0 Votes
    11 Posts
    6k Views
    M

    @VRonin said in Send arrow keys via QProcess in any platform:

    @Mark81 Then do you really need Qt? there are already mature tools to do those kind of things, for windows: http://ahkscript.org/ and for linux: http://www.semicomplete.com/projects/xdotool/

    I don't need Qt for this. But I have a quite large application that does a lot of things. One of those, is to launch external applications and send them keystrokes as you would type in front of a keyboard.