[Qt-interest] basic QScript question

Alexander Carôt Alexander_Carot at gmx.net
Wed Jun 30 20:54:28 CEST 2010


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