Pergunta

I'm trying to implement the possibility to script an existing QT application.

It works fine, but some function of my class return a TypeError.

Myclass.h (really simplified) :

class Myclass: public QObject
{
  Q_OBJECT
public slots:
  int firstfunction() const;
  int secondfunction() const;

private:
  int m_firstResult;
  int m_secondResult;
}

Myclass.cpp :

int Myclass::firstfunction() const
{
  return m_firstResult;
}

int Myclass::secondfunction() const
{
  return m_secondResult;
}

The main :

Myclass qtObjectClass();
QScriptEngine scriptEngine;

QScriptValue qValue= scriptEngine.newQObject(&qtObjectClass);
Q_ASSERT (qtObjectClass.isQObject());

scriptEngine.globalObject().setProperty("QTscriptEngine", qValue);

QFile file("testScript.js");
file.open(QIODevice::ReadOnly);
QScriptValue result = scriptEngine.evaluate(file.readAll());

if(result.toString() != "undefined")
  std::cout << result.toString().toStdString() << std::endl;

file.close();

if (scriptEngine.hasUncaughtException()) 
{
  int lineNo = scriptEngine.uncaughtExceptionLineNumber();
  printf("lineNo : %i", lineNo);
}

The script :

print(QTscriptEngine.firstfunction());

print(QTscriptEngine.secondfunction());

And the (strange) result :

5
TypeError: Result of expression 'QTscriptEngine.secondfunction' [1] is not a function.

Where 5 is the result of firstfunction() and [1] the result of secondfunction().

Of course, the result of my function is not a function, it's pretty normal, no ?

I don't understand why one is working when the other one is not (but evaluated because [1] is clearly the good returned value of secondfunction())

(I have really simplify all the code, and maybe the problem come from an other place, but it's clearly strange)

Any idea ? Thanks.

Foi útil?

Solução

The answer was stupid. I had properties defined like that :

Q_PROPERTY(int m_firstResult READ firstfunction)

The function who are in a Q_PROPERTY return a TypeError. And I havn't even put my properties in my question, so no one was able to answer my question, sorry for that.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top