[Qt-interest] basic QScript question

Constantin Makshin cmakshin at gmail.com
Sat Jul 3 17:09:52 CEST 2010


Obviously you can't work with scripts without script engine.

In your case, when QScriptEngine object is created inside sliderC class constructor, it's destroyed when the constructor returns control to the main() function. I.e. there's no script engine when you move your slider and therefore there's no way to call your function.

When your class is a QScriptEngine derivative, it acts as the script engine and program works as expected.

On Thursday 01 July 2010 14:14:30 Alexander Carôt wrote:
> > Because your "handler" object gets destroyed when sliderC class
> > constructor finishes its work (before the application enters the event loop)?
> 
> Thanks for the guess but if that was true it would imply that any other object is destroyed after having passed the constructor - which is obviously not the case.
> 
> However, in the meantime I could figure where the problem was: When my custom class inherited from QScriptEngine it actually worked out. The question "why" still remains though.
> 
> Best
> 
> -- A l e x
> 
> 
> 
> 
> 
> 
>  
> > On Wednesday 30 June 2010 22:54:28 Alexander Carôt wrote:
> > > Hello all,
> > > 
> > > I have basic understanding problem regarding QScript: In a simple
> > example I am dragging a QSlider, which in turn calls a QScript that plots the
> > respective fader position into the terminal.
> > > 
> > > The script looks like this:
> > > 
> > > (function give( value ){
> > >   print('Slider has value and returns:', value );
> > >   return value;
> > > })
> > > 
> > > and the respective main function looks like this:
> > > 
> > > ************
> > > int main( int argc, char* argv[] ){
> > >   QApplication app( argc, argv );
> > >   QWidget *GUI = new QWidget(); 
> > >   QSlider *slider = new QSlider();
> > >   QScriptEngine engine;
> > >   QString fileName = "translator.qs";
> > >   QFile scriptFile(fileName);
> > >   scriptFile.open(QIODevice::ReadOnly);
> > >   QTextStream stream(&scriptFile);
> > >   QString contents = stream.readAll();
> > >   scriptFile.close();
> > >   QScriptValue handler = engine.evaluate(contents,fileName);
> > >   qScriptConnect( slider, SIGNAL( valueChanged(int) ), QScriptValue(),
> > handler);
> > > }
> > > ***********
> > > 
> > > This works fine, however, when replacing the code from the main function
> > into a separated class the script is not called anymore:
> > > 
> > > 
> > > **********
> > > #include "sliderC.h"
> > > int main( int argc, char* argv[] ){
> > >   QApplication app( argc, argv );
> > > 
> > >   sliderC myScriptExample;
> > >  
> > >   return app.exec();
> > > }
> > > 
> > > #include "sliderC.h"
> > > sliderC::sliderC(){
> > >   QWidget *GUI = new QWidget(); 
> > >   QSlider *slider = new QSlider();
> > >   QScriptEngine engine;
> > >   QString fileName = "translator.qs";
> > >   QFile scriptFile(fileName);
> > >   scriptFile.open(QIODevice::ReadOnly);
> > >   QTextStream stream(&scriptFile);
> > >   QString contents = stream.readAll();
> > >   scriptFile.close();
> > >   QScriptValue handler = engine.evaluate(contents,fileName);
> > >   qScriptConnect( slider, SIGNAL( valueChanged(int) ), QScriptValue(),
> > handler);
> > > }
> > > ***********
> > > 
> > > Can anyone explain me why this is the case ? 
> > > 
> > > Thanks a lot in advance
> > > 
> > > -- A l e x



More information about the Qt-interest-old mailing list