67

I need to execute a system command in Qt and provide an argument for the command.

For example, opening a text file with gedit:

gedit /home/oDx/Documents/a.txt 

But the path /home/oDx/Documents/a.txt will be in a variable like docPath.

How can I achieve this?

3 Answers 3

94
QProcess process; process.start("gedit", QStringList() << docPath); 

the same as above

QProcess process; process.start("gedit", QStringList() << "/home/oDx/Documents/a.txt"); 

Also, read this.

Sign up to request clarification or add additional context in comments.

Comments

17

QProcess::execute() may be helpful, although is deprecated:

QProcess::execute("gedit /home/oDx/Documents/a.txt"); 

Comments

6

As of Qt 6.0, you can use QProcess::startCommand:

QProcess process; process.startCommand("gedit /home/oDx/Documents/a.txt"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.