简体   繁体   English

QProcess多平台命令

[英]QProcess Multiplatform command

I need to launch some script using QProcess. 我需要使用QProcess启动一些脚本。

For this, under windows, I use QProcess::execute("cmd [...]"); 为此,在windows下,我使用QProcess::execute("cmd [...]"); .

However, this won't work if I go under some other OS such as Linux. 但是,如果我使用其他操作系统(如Linux),这将无法工作。

So, I was wondering if the best solution to make that code portable, would be to interfere with a mutliplatform scripting solution, such as TCL for exemple. 所以,我想知道使代码可移植的最佳解决方案是干扰mutliplatform脚本解决方案,例如TCL例如。

So I use : QProcess:execute("tclsh text.tcl"); 所以我使用: QProcess:execute("tclsh text.tcl"); and it works. 它的工作原理。

But, I got three questions concerning that problem. 但是,我对这个问题有三个问题。 Because I'm not sure of what I've done. 因为我不确定我做了什么。

  • Will execute() execute tclsh with the file test.tcl both under Windows and Linux wherever I execute it ? 在我执行它的任何地方, execute()都会在Windows和Linux下使用test.tcl文件执行tclsh吗? It seems to do so, but I want to be sure ! 它似乎这样做,但我想确定! Is there any bad scenario that can happen ? 是否有可能发生的不良情况?
  • Is this a good solution ? 这是一个好的解决方案吗? I know lots of people have way more experience than I do, and I'd be grateful for anything I could learn ! 我知道很多人比我更有经验,我会感激任何我能学到的东西!
  • Why not using std::system() ? 为什么不使用std::system() Is it less portable ? 它不太便携吗?

While this isn't a total answer, I can point out a few things. 虽然这不是一个完整的答案,但我可以指出一些事情。

In particular, tclsh is quite happy under Windows; 特别是, tclsh在Windows下非常开心; it's a major supported platform. 它是一个主要的支持平台。 The main problem that could happen in practice is if you pass a filename with a space in it (this is distinctly more likely under Windows than on a Unix due to differences in community practice). 在实践中可能发生的主要问题是如果你传递一个带有空格的文件名(由于社区实践的不同,这在Windows下明显比在Unix上更可能)。 However, the execute() as you have written it has no problems. 但是, 你编写execute()没有问题。 Well, as long as tclsh is located on the PATH . 好吧,只要tclsh位于PATH

The other main option for integrating Tcl script execution with Qt is to link your program against the Tcl binary library and use that. 将Tcl脚本执行与Qt集成的另一个主要选项是将程序与Tcl二进制库链接并使用它。 Tcl's API is aimed at C, so it should be pretty simple to use from C++ (if a little clunky from a C++ perspective): Tcl的API是针对C的,所以从C ++开始使用它应该非常简单(如果从C ++的角度来看有点笨拙):

// This holds the description of the API
#include "tcl.h"

// Initialize the Tcl library; *call only once*
Tcl_FindExecutable(NULL);

// Make an evaluation context
Tcl_Interp *interp = Tcl_CreateInterp();

// Execute a script loaded from a file (or whatever)
int resultCode = Tcl_Eval(interp, "source test.tcl");

// Check if an error happened and print the error if it did
if (resultCode == TCL_ERROR) {
    std::cerr << "ERROR: " << Tcl_GetString(Tcl_GetObjResult(interp)) << std::endl;
}

// Squelch the evaluation context
Tcl_DeleteInterp(interp);

I'm not a particularly great C++ coder, but this should give the idea. 我不是一个特别优秀的C ++编码器,但这应该给出了这个想法。 I have no idea about QProcess::execute() vs std::system() . 我不知道QProcess::execute() vs std::system()

A weak point of your solution is that on windows you'll have to install tclsh . 解决方案的一个弱点是在Windows上你必须安装tclsh There is no tclsh on Solaris either. Solaris上也没有tclsh May be somewhere else. 可能是别的地方。

Compared to std::system() , QProcess gives you more control and information about the process of executing your command. std::system()相比, QProcess为您提供了有关执行命令过程的更多控制和信息。 If all you need is just to execute script (without receiving the output, for example) - std::system() is a good choice. 如果您只需要执行脚本(例如,不接收输出) - std::system()是一个不错的选择。

What I've used in a similar situation: 我在类似的情况下使用了什么:

#ifdef Q_OS_WIN
    mCommand = QString("cmd /C %1 %2").arg(command).arg(args);
#else
    mCommand = QString("bash %1 %2").arg(command).arg(args);
#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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