Results 1 to 8 of 8

Thread: Creating and executing cmd files

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Creating and executing cmd files

    Hello!

    I have in mind the following "program" to do:

    a guy click a button in, let us say, a MainWindow. A QDialog opens with an empty space to write text and a OK button comes together. In the white text field, the guy writes a windows or linux cmd code, and clicks "OK". When he does, the QDialog is closed, a function is called and creates a cmd file, put the text-code he wrote in it and its closed.

    Later, the guy clicks a different pushbutton in the MainWindow and a list of various cmd files he created before is shown in a QListWidget. He selects one, clicks a "OK" and the cmd file in questions is called and executed.


    My question is: how do I do that?

    Essentially I don't think there will be a problem with the window and list part. Essentially I create a class responsible for each "cmd object" and in the MainWindow a vector of that class. That each time the OK button is pressed, an object of the "cmd class" is created and the vector executes a pushback() of that object. When the second PushButton is pressed and a QListWidget appears, it uses a for(; to take all the "cmd objects" created till than and present them as an Item of the list. When the "OK" button is pressed, a connect() with the MainWindow is "called" and a function is executed, namely a function that opens and executes the cmd file in question.


    The problem is that I don't know how to do the create/execute part of the cmd file. I have a bit of experience in writing and reading .txt files with QFile and QTextStream, but I don't know how to do something similar with cmd files. Nor I know if the text-code put by the user in the white text place should be copied in a big QString and than put in the cmd, or if a should use another method.


    Could somebody help me, please?

    Thanks!


    Momergil

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Creating and executing cmd files

    If by "cmd files" you mean script files for command interpreters (such as cmd on Windows and Bash on Unix) then those are regular text files so you can use the same approach. As for executing them, either use the system() call or QProcess.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating and executing cmd files

    Quote Originally Posted by wysota View Post
    If by "cmd files" you mean script files for command interpreters (such as cmd on Windows and Bash on Unix) then those are regular text files so you can use the same approach. As for executing them, either use the system() call or QProcess.
    So, in other words, I do exactly the same, only in the part where I define the file instead I write something as "./test/test.txt" I write "./test/test.cmd" and everything else is the same?

    Thanks!

  4. #4
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating and executing cmd files

    I do not understand your question. Please clarify. There is no such thing as a .cmd file anyway. Perhaps you should first learn how to write scripts for the command interpreter. Under Windows, you will use the .bat extension. Under GNU/Linux, you will need to make the file executable and have it start with the proper shebang.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Creating and executing cmd files

    Yes, it's the same. At least regarding the part where you define scripts.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating and executing cmd files

    I do not understand your question. Please clarify. There is no such thing as a .cmd file anyway. Perhaps you should first learn how to write scripts for the command interpreter. Under Windows, you will use the .bat extension. Under GNU/Linux, you will need to make the file executable and have it start with the proper shebang.
    yeye, now I know that there is not such thing as a .cmd file ^^ I though there was, but I saw a video about this .bat in YouTube and now I know. Thanks! Now, about Linux, what exactly should I do?

    Yes, it's the same. At least regarding the part where you define scripts.
    O.K., so I just change the file type from txt to .bat. Thanks!

  7. #7
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating and executing cmd files

    Now, about Linux, what exactly should I do?
    You need to do two things:
    1. Add a line (the so-called "shebang") at the beginning of the script specifying which interpreter should run it; here is an example of a Hello world script for sh:
    Qt Code:
    1. #!/bin/sh
    2. echo "Hello world"
    To copy to clipboard, switch view to plain text mode 
    Depending on what you want to do you might leave that to the user of your application.
    2. Make the script executable by its owner. See QFile::setPermissions().

  8. #8
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating and executing cmd files

    Quote Originally Posted by yeye_olive View Post
    You need to do two things:
    1. Add a line (the so-called "shebang") at the beginning of the script specifying which interpreter should run it; here is an example of a Hello world script for sh:
    Qt Code:
    1. #!/bin/sh
    2. echo "Hello world"
    To copy to clipboard, switch view to plain text mode 
    Depending on what you want to do you might leave that to the user of your application.
    2. Make the script executable by its owner. See QFile::setPermissions().
    yeye,

    thanks for the help, and thanks for all the others.


    God bless,

    Momergil.

Similar Threads

  1. Open/Executing Files on Linux
    By MBex in forum Qt Programming
    Replies: 5
    Last Post: 3rd June 2011, 00:15
  2. Creating pkg-config files
    By zarq in forum Installation and Deployment
    Replies: 0
    Last Post: 6th May 2010, 11:53
  3. Replies: 1
    Last Post: 24th October 2009, 15:11
  4. QFile and Creating Files
    By Jingoism in forum Newbie
    Replies: 2
    Last Post: 28th July 2007, 17:11
  5. uic not creating seperate .h and .cpp files
    By Kapil in forum Qt Programming
    Replies: 2
    Last Post: 13th February 2006, 10:43

Tags for this Thread

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.