Date with Qt script



  • Hello

    With Qt script i would like to test the day number.
    This is my code:

    QScriptValue script = myEngine.newDate(QDateTime::currentDateTime());
    myEngine.globalObject().setProperty("date", script);
    
    myEngine.evaluate("date.day() == 5").toBool();
    

    but it doesn't work.
    I don't know why.
    Thank you for your help.



  • @Snyfir
    In http://doc.qt.io/qt-5/qscriptvalue.html#details I see:

    QScriptValue supports the types defined in the ECMA-262 standard: The primitive types, which are Undefined, Null, Boolean, Number, and String; and the Object type. Additionally, Qt Script has built-in support for QVariant, QObject and QMetaObject.

    For the object-based types (including Date and RegExp), use the newT() functions in QScriptEngine (e.g. QScriptEngine::newObject()) to create a QScriptValue of the desired type. For the primitive types, use one of the QScriptValue constructor overloads.

    So to me that implies you need to respect the second paragraph for your object to be treated as a Date?



  • @JNBarchan
    What do you mean treated as a Date ? do you have an example ?



  • @Snyfir
    I have no example as I've never even used Qt Script! But your QScriptValue script = myEngine.newDate(QDateTime::currentDateTime()); is assigning a value of type Date, which you will later want to perform date-type operation on (date.day()). And it seems to me the manual is saying you need to do some kind of QScriptEngine::newObject()) for Dates.

    Oh, I'm so sorry, is that what your myEngine.newDate() is already doing? My apologies if it is.

    I would at least suggest you evaluate date.day() alone first to see what it is returning, without the == 5 test...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.