Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Date with Qt script
Forum Update on Tuesday, May 27th 2025

Date with Qt script

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 909 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Snyfir
    wrote on 6 Oct 2017, 08:04 last edited by
    #1

    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.

    J 1 Reply Last reply 6 Oct 2017, 08:49
    0
    • S Snyfir
      6 Oct 2017, 08:04

      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.

      J Offline
      J Offline
      JonB
      wrote on 6 Oct 2017, 08:49 last edited by
      #2

      @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?

      S 1 Reply Last reply 6 Oct 2017, 10:37
      0
      • J JonB
        6 Oct 2017, 08:49

        @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?

        S Offline
        S Offline
        Snyfir
        wrote on 6 Oct 2017, 10:37 last edited by
        #3

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

        J 1 Reply Last reply 6 Oct 2017, 10:45
        0
        • S Snyfir
          6 Oct 2017, 10:37

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

          J Offline
          J Offline
          JonB
          wrote on 6 Oct 2017, 10:45 last edited by
          #4

          @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...

          1 Reply Last reply
          0

          1/4

          6 Oct 2017, 08:04

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved