QtScript Output Redirection

When I call this code:

QScriptEngine e; e.evaluate("print('hello, world!')"); 

the output text (from the printing method) is written to the main application terminal.

Is there a way to redirect it to a custom QIODevice?

+4
source share
2 answers

You can replace print() your own implementation:

First, define a C ++ function that does what you want. In this case, it is simply empty for presentation:

 QScriptValue myPrint( QScriptContext * ctx, QScriptEngine * eng ) { return QScriptValue(); } 

Then set this function as the new print() :

 QScriptEngine e = ...; e.globalObject().setProperty( "print", e.newFunction( &myPrint ) ); e.evaluate( "print(21);" ); // prints nothing 
+5
source

The output is output to stdout, so you need to redirect stdout. For ideas, see. This is a question . Best ideas: try redirecting to FILE * again or (better) use rdbuf to redirect stdout to another stream obtained from std :: ostream and you can play with QFile.open (1, ...) -

+1
source

Source: https://habr.com/ru/post/1334471/


All Articles