當我把這種代碼:QtScript輸出重定向
QScriptEngine e;
e.evaluate("print('hello, world!')");
輸出文本(從打印方法)寫入到主應用程序的終端。
有沒有辦法將它重定向到一個自定義的QIODevice?
當我把這種代碼:QtScript輸出重定向
QScriptEngine e;
e.evaluate("print('hello, world!')");
輸出文本(從打印方法)寫入到主應用程序的終端。
有沒有辦法將它重定向到一個自定義的QIODevice?
您可以用您自己的實現替換print()
:
首先,定義一個C++函數,它可以實現您想要的功能。
QScriptValue myPrint(QScriptContext * ctx, QScriptEngine * eng) {
return QScriptValue();
}
然後安裝該功能爲您的新print()
:
QScriptEngine e = ...;
e.globalObject().setProperty("print", e.newFunction(&myPrint));
e.evaluate("print(21);"); // prints nothing
輸出文本轉到標準輸出,因此您需要重定向標準輸出。有關想法,請參閱question。最好的想法:使用重新打開重定向到FILE *,或者(更好)使用rdbuf將stdout重定向到std :: ostream派生的其他流,並且您可以使用QFile.open(1,...) -
這裏有大量的文本,去到stdout(QWarnings,QDebug到stderr,其他在這種情況下,它只是爲空博覽會printf/scanf等)。我只需要重定向QScriptEngine的輸出...... – Arenim 2011-01-07 12:22:40