Results 1 to 7 of 7

Thread: Execute system command from QProcess

  1. #1
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Execute system command from QProcess

    Hi,
    I was writing a small piece of code to execute some system commands.
    I'm using Debian, i changed GDM3 to allow root access (no security issues needed in this case) and the code is:
    Qt Code:
    1. void criarWindowsDialog::on_pushButton_2_clicked()
    2. {
    3. ui->commandTextEdit->append("Processo iniciado ...");
    4. QProcess processo;
    5. QString comando;
    6. QString fullCommand;
    7. QString stdOut;
    8. QString stdError;
    9.  
    10. //comando 1 - limpar a pasta
    11. comando = "rm";
    12. args.clear();
    13. args<<"-f"<<"/home/srp/imagens/windows/*";
    14. fullCommand = comando;
    15. for(int i = 0; i < args.count(); i++)
    16. fullCommand += " " + args.at(i);
    17. processo.start(comando,args,QIODevice::ReadOnly);
    18. processo.waitForFinished();
    19. stdOut = processo.readAllStandardOutput();
    20. stdError = processo.readAllStandardError();
    21. ui->commandTextEdit->append(fullCommand);
    22. ui->stdOutTextEdit->append(stdOut);
    23. ui->stdErrorTextEdit->append(stdError);
    To copy to clipboard, switch view to plain text mode 

    When i check the message from "stdError" i get:
    Qt Code:
    1. Failed to open terminal.TERM environment variable needs set.
    To copy to clipboard, switch view to plain text mode 

    This is a simple command to delete files inside a folder.
    Is the problem related to configuration in debian?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Execute system command from QProcess

    Hmm, strange, rm does't sounds like a program that needs a terminal.

    Anyway, set the TERM environment variable as suggested, see QProcess::setProcessEnvironment().

    In that particular case, removing files, you could also do that directly.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    graciano (20th February 2014)

  4. #3
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Execute system command from QProcess

    The same thing happens when i try ...
    Qt Code:
    1. comando = "partimage";
    2. args.clear();
    3. args<<"-z1"<<"-o"<<"-d"<<"-b"<<"-save"<<"/dev/sda1 /home/srp/imagens/windows/windows.gz";
    4. fullCommand = comando;
    5. for(int i = 0; i < args.count(); i++)
    6. fullCommand += " " + args.at(i);
    7. processo.start(comando,args,QIODevice::ReadOnly);
    8. processo.waitForFinished();
    9. stdOut = processo.readAllStandardOutput();
    10. stdError = processo.readAllStandardError();
    11. ui->commandTextEdit->append(fullCommand);
    12. ui->stdOutTextEdit->append(stdOut);
    13. ui->stdErrorTextEdit->append(stdError);
    To copy to clipboard, switch view to plain text mode 

    ... only now i'm executing "partimage".
    I used QProcess::setProcessEnvironment() to set TERM=xterm but i get the same errors.

    By the way, the QProcess::setProcessEnvironment() is labeled as depretated. What does it mean? Not to use??

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Execute system command from QProcess

    setEnvironment() is deprecated, setProcessEnvironment is not.

    Try to execute something simple that evaluates the TERM variable, e.g.

    echo $TERM

    Does this give the correct result?

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    graciano (20th February 2014)

  7. #5
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Execute system command from QProcess

    I tried this:
    Qt Code:
    1. env.insert("TERM", "xterm");
    2. processo.setProcessEnvironment(env);
    3.  
    4. //comando 1 - limpar a pasta
    5. comando = "echo";
    6. args.clear();
    7. args<<"$TERM";
    8. fullCommand = comando;
    9. for(int i = 0; i < args.count(); i++)
    10. fullCommand += " " + args.at(i);
    11. processo.start(comando,args,QIODevice::ReadOnly);
    12. processo.waitForFinished();
    13. stdOut = processo.readAllStandardOutput();
    14. stdError = processo.readAllStandardError();
    15. ui->commandTextEdit->append(fullCommand);
    16. ui->stdOutTextEdit->append(stdOut);
    17. ui->stdErrorTextEdit->append(stdError);
    To copy to clipboard, switch view to plain text mode 

    Using a system console i get:
    Qt Code:
    1. root@srp:~# echo $TERM
    2. xterm
    To copy to clipboard, switch view to plain text mode 

    Using my code, in the ui->stdOutTextEdit e read:
    Qt Code:
    1. $TERM
    To copy to clipboard, switch view to plain text mode 

  8. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Execute system command from QProcess

    Right, sorry, echo $TERM won't work because there is no shell expanding $TERM.

    You could try with a simple shell script as the command, no args needed

    Qt Code:
    1. #!/bin/sh
    2. echo $TERM
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  9. The following user says thank you to anda_skoa for this useful post:

    graciano (20th February 2014)

  10. #7
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Execute system command from QProcess

    Using a shell script i get the expected output:
    Qt Code:
    1. comando = "bash";
    2. args.clear();
    3. args<<"/home/srp/imagens/scripts/criarwindows.sh";
    To copy to clipboard, switch view to plain text mode 

    Where does it lead us?

Similar Threads

  1. How to execute Powershell command from QT?
    By Gokulnathvc in forum Newbie
    Replies: 3
    Last Post: 21st January 2014, 23:30
  2. Execute window command line-by-line using QProcess
    By nhocjerry in forum Qt Programming
    Replies: 5
    Last Post: 30th July 2013, 12:21
  3. Replies: 3
    Last Post: 23rd February 2011, 08:50
  4. Replies: 4
    Last Post: 14th July 2009, 03:27
  5. System Command QProcess
    By jd in forum Qt Programming
    Replies: 7
    Last Post: 10th February 2009, 17:36

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.