[Qt-interest] basic QScript question

Constantin Makshin cmakshin at gmail.com
Wed Jun 30 22:24:38 CEST 2010


Because your "handler" object gets destroyed when sliderC class constructor finishes its work (before the application enters the event loop)?

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