Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 5

Qt-interest Archive, February 2008
QtScript - hooking into "print"


Message 1 in thread

The QtScript docs show numerous examples that use print() to print 
things, but where does the output go? How do I tie into that so I can 
send the results to my QTextEdit-based console? I was looking for a 
signal to connect to but there doesn't seem to be one. What am I missing?

-- 
 [ signature omitted ] 

Message 2 in thread

Not that it's "the right way" ...
but I created my own print function:

void
IqtScriptKit::setupPrint()
{
    // make "print" print to console not terminal.

    // Map existing print function to terminalPrint
    QScriptValue rtn;
    QString toRun("terminalPrint = print;");
    d_inScript = true;
    rtn = d_scriptEngine->evaluate(/*program*/ toRun,
                                   /*fileName*/ "<builtIn>",
                                   /*lineNumber*/ 0);
    d_inScript = false;
    if ( d_scriptEngine->hasUncaughtException() ) {
        d_console.statusOut(IqtErr) << "IqtScriptKit::setupPrint():" << endl
            << "Unexpected exception" << endl;
        d_console.reportBacktrace(toRun,rtn);
    } else {
        if ( 0 ) {
            d_console.statusOut(IqtDebug)
                << "IqtScriptKit::setupPrint():" << endl
                << "terminalPrint assign returns " << rtn.toString() << 
endl;
        }
    }

    // put our print function into script env:
    addCplusplusFunc("print",IqtScriptKit::consolePrint);
}
//static
QScriptValue
IqtScriptKit::consolePrint(QScriptContext* context,QScriptEngine* 
/*engine*/)
{
    QScriptValue arg = context->argument(0);
    if (!arg.isString()) {
        return context->throwError(
            QScriptContext::TypeError,
            QString::fromLatin1("print(): expected string argument"));
    }
    QString toPrint = arg.toString();
    IqtConsole::statusOut(IqtInfo) << toPrint << endl;
    IqtConsole::statusOut(IqtInfo).flush();
    return QScriptValue();
}



Paul Miller wrote:
> The QtScript docs show numerous examples that use print() to print 
> things, but where does the output go? How do I tie into that so I can 
> send the results to my QTextEdit-based console? I was looking for a 
> signal to connect to but there doesn't seem to be one. What am I missing?
>

--
 [ signature omitted ] 

Message 3 in thread

Peter Hackett wrote:
> Not that it's "the right way" ...
> but I created my own print function:
> 

Thanks Peter. I'm surprised something like this isn't part of QtScript.

-- 
 [ signature omitted ] 

Message 4 in thread

http://lists.trolltech.com/qt-interest/2008-02/thread00515-0.html

On Tue, 26 Feb 2008 23:41:30 +0300, Paul Miller <paul@xxxxxxxxxx> wrote:
> The QtScript docs show numerous examples that use print() to print  
> things, but where does the output go? How do I tie into that so I can  
> send the results to my QTextEdit-based console? I was looking for a  
> signal to connect to but there doesn't seem to be one. What am I missing?

-- 
 [ signature omitted ] 

Message 5 in thread

Constantin Makshin wrote:
> http://lists.trolltech.com/qt-interest/2008-02/thread00515-0.html
> 
> On Tue, 26 Feb 2008 23:41:30 +0300, Paul Miller <paul@xxxxxxxxxx> wrote:
>> The QtScript docs show numerous examples that use print() to print 
>> things, but where does the output go? How do I tie into that so I can 
>> send the results to my QTextEdit-based console? I was looking for a 
>> signal to connect to but there doesn't seem to be one. What am I missing?

Aha! I knew it had to be "simple". Thanks for the pointer. I'm usually 
pretty good at my google queries but my efforts didn't turn that one up.

-- 
 [ signature omitted ]