[Qt-interest] QScript Newbie Question
Sean Harmer
sean.harmer at maps-technology.com
Mon Feb 8 14:15:02 CET 2010
Hi,
On Monday 08 February 2010 11:59:38 Alexander Carôt wrote:
> Hello Kent,
>
> thanks for the reply ! However, my problem is possibly more "newby" than it
> appears: I am still on 4.5.3, where the "bracket"-problem doesn't exist. I
> simply wonder why my code actually does nothing:
>
> I am connecting a line-edit-signal to the a script-slot as follows:
>
> QScriptEngine engine;
> QLineEdit *edit = new QLineEdit("",this);
> edit->setGeometry(0,0,50,23);
> edit->show();
> QScriptValue handler = engine.evaluate("function (text){print('text was
> changed to',text);}"); qScriptConnect(edit, SIGNAL(textChanged(const
> QString &)), QScriptValue(), handler); qDebug() << handler.toString();
>
> but whenever I type anything into the edit field nothing happens. I am
> expecting the script to print the changed text into the terminal but
> neither a text output nor an error message appears. Nevertheless, the last
> qDebug-call plots the following into the terminal:
>
> "function(text) {
> print("text was changed to", text);
> }"
>
> Since these are my first steps with QScript I am still unsure how to verify
> this in order to achieve the desired script-print-action. Hence, any help
> appreciated of course.
I may be being really dumb as I have only just scratched the surface of
QtScript. However, I think that your QScriptEngine and QScriptValue objects
could be going out of scope and getting destroyed. I think this because of the
"this" pointer that you pass to the QLineEdit c'tor.
The following application works for me (Qt 4.6.1):
#include <QApplication>
#include <QDebug>
#include <QLineEdit>
#include <QScriptEngine>
int main( int argc, char* argv[] )
{
QApplication app( argc, argv );
QLineEdit* edit = new QLineEdit();
edit->setGeometry( 0, 0, 150, 23 );
edit->show();
QScriptEngine engine;
QScriptValue handler = engine.evaluate( "(function myFunc( text ) { print(
'Text changed to:', text ); })" );
qScriptConnect( edit, SIGNAL( textChanged( const QString& ) ),
QScriptValue(), handler );
qDebug() << handler.toString();
return app.exec();
}
To embed this within another object as will likely be the case, just create
the QScriptEngine object on the heap so that it is not destroyed.
HTH,
Sean
More information about the Qt-interest-old
mailing list