简体   繁体   中英

Run Qt QProcess independently on the OS

I am developing an application on QT in C++ and I learned recently how to use it, therefore I am still not totally confident. I am struggling with this: I am developing on Linux but I would like to make it work also on Windows. The C++ code calls a Python script (passing some arguments to it), and to achieve my aim I am using

QDir::separator()

to separate the folders names(the arguments list contains some paths), and this should be useful to my purpose, because it will use automatically / for Linux and \\ on Windows (correct me if I am wrong).

The code looks like this:

python_path= my_python_path/python;
script_path=../script.py;


QStringList arguments;
arguments << QString(script_path);
arguments << QString(arg1);
arguments << QString(arg2);


QProcess *p = new QProcess( this );

if (p){

  p->start(python_path,arguments);
}

Now my problem: on Windows I should use:

 python_path= my_python_path/python.exe;

instead of:

 python_path= my_python_path/python;

How to treat this multi platform development on QT? Detect the system and use a if? Or is there some better solution?

Thanks in advance!

You can use Q_OS macros. Something like that should works (not tested):

#if defined Q_OS_WIN
QString python_path = "my_python_path/python.exe";
#else
QString python_path = "my_python_path/python";
#endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM