Results 1 to 6 of 6

Thread: Not understanding QtScript

  1. #1
    Join Date
    Apr 2012
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Not understanding QtScript

    I'm trying to learn about QT script and I'm clearly not understanding something basic.
    Consider the following code:

    Qt Code:
    1. #include <iostream>
    2.  
    3. #include <QApplication>
    4. #include <QtScript>
    5. #include <QObject>
    6.  
    7. using namespace std;
    8.  
    9. class MyClass : public QObject
    10. {
    11. public:
    12. int x;
    13. };
    14.  
    15. int main(int argc, char* argv[])
    16. {
    17. QApplication app(argc, argv);
    18.  
    19. MyClass mc;
    20. mc.x = 0;
    21.  
    22. QScriptEngine eng;
    23. QScriptValue scriptClass = eng.newQObject(&mc);
    24. QScriptValue global = eng.globalObject();
    25. global.setProperty("myClass", scriptClass);
    26.  
    27. QScriptValue res = eng.evaluate("myClass.x == 0");
    28. if (res.isBoolean())
    29. {
    30. cout << "res is 0 = " << res.toBoolean() << endl;
    31. }
    32. else
    33. {
    34. cout << "res is not boolean!!!" << endl;
    35. }
    36. return 0;
    37. }
    To copy to clipboard, switch view to plain text mode 

    This prints "res is 0 = 0" when run. It seems that the script engine does know about myClass but the value of myClass.x is not set.
    Can someone enlighten me?

    Thanks,
    Steve

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Not understanding QtScript

    "x" needs to be a regular Qt property for this to work.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2012
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default [Solved] Re: Not understanding QtScript

    Thanks wysota!

    Changed MyClass to

    Qt Code:
    1. class MyClass : puble QObject
    2. {
    3. Q_PROPERTY(int x);
    4. };
    To copy to clipboard, switch view to plain text mode 

    and
    Qt Code:
    1. mc.x = 0;
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. mc.setProperty("x", 0);
    To copy to clipboard, switch view to plain text mode 

    and now its working.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [Solved] Re: Not understanding QtScript

    That's not entirely correct. Should be:

    Qt Code:
    1. class MyClass : public QObject
    2. Q_OBJECT
    3. Q_PROPERTY(int x READ x WRITE setX)
    4. public:
    5. MyClass(QObject *parent = 0) : QObject(parent) { m_x = 0; }
    6. int x() const { return m_x; }
    7. public slots:
    8. void setX(int _x) { m_x = _x; }
    9. private:
    10. int m_x;
    11. };
    12.  
    13. mc.setX(0);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Apr 2012
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: [Solved] Re: Not understanding QtScript

    It seems that what I did caused some dynamic properties to be created so that my program gave the answers I expected without actually being correct.
    See my other post on Q_PROPERTY and Q_OBJECT.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [Solved] Re: Not understanding QtScript

    I know, thus if you use my code above, it will work as expected.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Understanding QNetworkConfigurationManager
    By TheIndependentAquarius in forum Qt Programming
    Replies: 5
    Last Post: 21st September 2011, 12:47
  2. QUdpSocket understanding
    By ale301168 in forum Qt Programming
    Replies: 4
    Last Post: 7th July 2011, 07:33
  3. Understanding RGB888
    By scarleton in forum Qt Programming
    Replies: 6
    Last Post: 29th August 2010, 20:03
  4. Replies: 0
    Last Post: 25th November 2009, 07:46
  5. I need help understanding QGraphicsView
    By aarelovich in forum Qt Programming
    Replies: 13
    Last Post: 22nd July 2009, 20:02

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.