Results 1 to 13 of 13

Thread: Use cmd commands in QT

  1. #1
    Join Date
    Sep 2016
    Posts
    78
    Thanked 1 Time in 1 Post
    Qt products
    Qt5

    Default Use cmd commands in QT

    Hi i was checking and anyone can use commands very similar in cmd like dir mkdir etc.
    But for example when i try to use command (cd ..) i couldn't
    Code:
    Qt Code:
    1. QProcess consola;
    2. consola.start("cmd.exe /C " + comando);
    3. consola.waitForFinished();
    4. consola.waitForReadyRead();
    To copy to clipboard, switch view to plain text mode 

  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: Use cmd commands in QT

    First, when your command needs arguments, like here, use the start() function that takes a list of arguments instead.

    Second, you have forgotten to include the error you are getting.

    Cheers,
    _

  3. #3
    Join Date
    Sep 2016
    Posts
    78
    Thanked 1 Time in 1 Post
    Qt products
    Qt5

    Default Re: Use cmd commands in QT

    the error is when i do
    comando= "cd .."
    consola.start(comando);
    consola.waitForFinished();
    consola.waitForReadyRead();

    Nothing happen. it's this my problem thx in advance.
    I want to move since cmd console. but i am stand in the same path.

    One question if for example i pass the next string = "cd C:\"
    how i can take next argument to use like a path.

    the code that i have
    Qt Code:
    1. QProcess consola;
    2. #ifdef Q_OS_WIN
    3. if(comando=="cd .."){
    4. directorio.cd("..");
    5. }else{
    6. consola.start("cmd.exe /C " + comando);
    7. }
    8. #else
    9. consola.start(comando);
    10. #endif
    11. consola.waitForFinished();
    12. consola.waitForReadyRead();
    To copy to clipboard, switch view to plain text mode 

    Directorio is the folder where i am. obviously i need to put inside of directorio.cd the path but if i write for example this string in comando = "cd C:\" how i can get the path???

    at the momento not work. and the directorio.cd(".."); doesnt work too. when i do this show me this message QIODevice::read (QProcess): device not open
    Last edited by davinciomare; 6th October 2016 at 16:34.

  4. #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: Use cmd commands in QT

    Quote Originally Posted by davinciomare View Post
    the error is when i do
    comando= "cd .."
    consola.start(comando);
    consola.waitForFinished();
    consola.waitForReadyRead();

    Nothing happen. it's this my problem thx in advance.
    You don't even look at the error.

    But anyhow, even if that executes correctly, what what that be good for?
    You start cmd.exe and tell it to change its working directory, then it ends?

    Quote Originally Posted by davinciomare View Post
    One question if for example i pass the next string = "cd C:\"
    how i can take next argument to use like a path.
    Next as in after "C:\"?

    Quote Originally Posted by davinciomare View Post
    Directorio is the folder where i am. obviously i need to put inside of directorio.cd the path but if i write for example this string in comando = "cd C:\" how i can get the path???
    If that is the command then it is absolutely useless, right?

    You just make the cmd program switch its path and then it ends.

    Cheers,
    _

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Use cmd commands in QT

    You just make the cmd program switch its path and then it ends.
    I think davinciomare does not understand that his QProcess creates a new console, which executes his commands and then exits. The commands are not executed in the console where he runs this program, but in a new one that exists only for the time his program is running. The new console has no effect on his current console.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #6
    Join Date
    Sep 2016
    Posts
    78
    Thanked 1 Time in 1 Post
    Qt products
    Qt5

    Default Re: Use cmd commands in QT

    Stranz you are right because when i do dir .. show me the previous folder but for something qprocess créate all time a new console so i coudnt remember.... the old path.........
    Last edited by davinciomare; 6th October 2016 at 19:20.

  7. #7
    Join Date
    Sep 2016
    Posts
    78
    Thanked 1 Time in 1 Post
    Qt products
    Qt5

    Default Re: Use cmd commands in QT

    Quote Originally Posted by d_stranz View Post
    I think davinciomare does not understand that his QProcess creates a new console, which executes his commands and then exits. The commands are not executed in the console where he runs this program, but in a new one that exists only for the time his program is running. The new console has no effect on his current console.
    i define console like global but happen the same.
    Last edited by davinciomare; 7th October 2016 at 00:51.

  8. #8
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Use cmd commands in QT

    Quote Originally Posted by davinciomare View Post
    so i dont close the console????? this is the solution? thx in advance
    I don't think that's what he said at all. Run "cmd /?" and read the help for the /C option. It runs a single command and exits. Stringing together multiple executions like:
    Qt Code:
    1. cmd.exe /C cd /some/path
    2. cmd.exe /C cd ..
    To copy to clipboard, switch view to plain text mode 
    Runs two different process that operate completely independently. I.e. each process will get its own environment variables, one of which will be the current working directory (typically your user home directory, etc). Changing the current directory with the first command has no bearing on the current working directory of the 2nd, because their each independent processes and start/finish, etc.

    What I suspect you want to do, is write a Windows cmd script that you can then execute with QProcess. The cmd script would include all of the steps you wish to execute. You would write/test this script from your Windows command line and once it does exactly what you want, then you can attempt to execute that script via QProcess.

    Hope that helps.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  9. #9
    Join Date
    Sep 2016
    Posts
    78
    Thanked 1 Time in 1 Post
    Qt products
    Qt5

    Default Re: Use cmd commands in QT

    i can't to do two at the same time two pointers at the same time can't be added say me qt. i can change path only that qprocess create me all time a new console.. this is the problem..

  10. #10
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Use cmd commands in QT

    Tell us what You want to do using QProcess ?

  11. #11
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Use cmd commands in QT

    Quote Originally Posted by davinciomare View Post
    i can't to do two at the same time two pointers at the same time can't be added say me qt. i can change path only that qprocess create me all time a new console.. this is the problem..
    Sorry, I have no idea what you're trying to say. By using QProcess, it will always create a new process (that's what it's used for). If you don't want to create a new process (console), don't use QProcess???
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  12. #12
    Join Date
    Sep 2016
    Posts
    78
    Thanked 1 Time in 1 Post
    Qt products
    Qt5

    Default Re: Use cmd commands in QT

    I want to have the same console i can change the path with setWorkingDirectory but i think will not remember the same path.... when i again do one command again ñ.ñ

  13. #13
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Use cmd commands in QT

    Quote Originally Posted by davinciomare View Post
    I want to have the same console i can change the path with setWorkingDirectory but i think will not remember the same path.... when i again do one command again ñ.ñ
    If you're trying to use QProcess to execute command like "cd", then you clearly don't understand what you're doing. Sorry for being so blunt, but you have to understand what you're trying to do and how to accomplish that goal.

    Why don't you list all of the things you expect to be able to do via QProcess? My guess is all of those things should be done in a script and you should be using QProcess to execute the script, not each individual command.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. QSQLQUERY commands
    By arturs in forum Newbie
    Replies: 6
    Last Post: 11th June 2016, 22:16
  2. opengl commands
    By sajis997 in forum Newbie
    Replies: 0
    Last Post: 15th December 2014, 19:03
  3. Executing AT commands
    By jay in forum Qt Programming
    Replies: 4
    Last Post: 8th December 2009, 09:49
  4. Mac Commands
    By shyam prasad in forum Qt Programming
    Replies: 1
    Last Post: 9th January 2007, 15:30
  5. Replies: 2
    Last Post: 31st January 2006, 14: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.