Results 1 to 6 of 6

Thread: Testing app with Qt Script

  1. #1
    Join Date
    Jan 2007
    Location
    Rome
    Posts
    30
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Testing app with Qt Script

    Dear all,
    I would like to use QtScript in order to test a data model for an application, try all the methods, and the behaviour of the GUI during the modification of the data model.

    To do this, I would like to create a script console in which the developer could create and use some QObject child (classes that represents the data model) using script.

    I read the documentation and I understand well that I can use any instance of data model classes with this method:

    Qt Code:
    1. void ScriptConsole::addQObjectInstance(QObject *object, const QString &name)
    2. {
    3. QScriptValue objectValue = m_engine.newQObject(object);
    4. m_engine.globalObject().setProperty(name, objectValue);
    5. }
    To copy to clipboard, switch view to plain text mode 

    How can I use some classes of the data model inside the script ?
    I need to define, in some way, the possibility to create new objects of particular classes (that inherit QObject) in the script.

    Thanks in advance,

    Angelo

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

    Default Re: Testing app with Qt Script

    In your script you can create objects using new.
    javascript Code:
    1. function xxx()
    2. {
    3. var obj = new MyObject();
    4. // connect a script function to the signal
    5. obj["enabledChanged(bool)"].connect(enabledChangedHandler);
    6. obj.enabled = true;
    7. print( "obj is enabled: " + obj.enabled );
    8. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2007
    Location
    Rome
    Posts
    30
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Testing app with Qt Script

    I know that I can use new, but it doesn't worked as expected.

    Suppose that I have a class called Scene, that contains a QList of another class called Panel.
    Both Scene and Panel inherith QObject and have all their public methods available as public slots.

    If I write:

    Qt Code:
    1. var s = new Scene;
    To copy to clipboard, switch view to plain text mode 

    I receive the following error:

    Qt Code:
    1. ReferenceError: Scene is not defined
    To copy to clipboard, switch view to plain text mode 

    I think it is normal since in the global script environment I didn't define any custom type for Panel and Scene classes.

    After creating a new Scene object I would like to use its methods and, if it is possible, add or remove (in the Scene container) new Panel objects created using the script.

    Someone has tried to use Qt Script to create and "manage" C++ Objects ?
    Best,

    Angelo

  4. #4
    Join Date
    Jan 2007
    Location
    Rome
    Posts
    30
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Testing app with Qt Script

    I have found the solution:

    http://trolltech.com/developer/knowl...25.9557303148/

    Best regards,

    Angelo

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

    Default Re: Testing app with Qt Script

    Well, this is what is written in the docs for the QtScript module:
    Making a C++ object available to Scripts Written in Qt Script

    Making C++ classes and objects available to a scripting language is not trivial because scripting languages tend to be more dynamic than C++, and it must be possible to introspect objects (query information such as function names, function signatures, properties, etc., at run-time). Standard C++ does not provide features for this.
    We can achieve the functionality we want by extending C++, using C++'s own facilities so our code is still standard C++. The Qt meta-object system provides the necessary additional functionality. It allows us to write using an extended C++ syntax, but converts this into standard C++ using a small utility program called moc (Meta-Object Compiler). Classes that wish to take advantage of the meta-object facilities are either subclasses of QObject, or use the Q_OBJECT macro. Qt has used this approach for many years and it has proven to be solid and reliable. Qt Script uses this meta-object technology to provide scripters with dynamic access to C++ classes and objects.
    To completely understand how to make C++ objects available to Qt Script, some basic knowledge of the Qt meta-object system is very helpful. We recommend that you read the Qt Object Model. The information in this document and the documents it links to are very useful for understanding how to implement application objects.
    However, this knowledge is not essential in the simplest cases. To make an object available in Qt Script, it must derive from QObject. All classes which derive from QObject can be introspected and can provide the information needed by the scripting engine at run-time; e.g., class name, functions, signatures. Because we obtain the information we need about classes dynamically at run-time, there is no need to write wrappers for QObject derived classes.
    I have never used QtScript myself, but I think that the worst case scenario is that you'll need to access the objects meta-object and marshall the item through it, although from what I see in the above mentioned docs, you shouldn't need to do that.

  6. #6
    Join Date
    Jan 2007
    Location
    Rome
    Posts
    30
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Testing app with Qt Script

    I have another problem

    In the Scene class to retrieve a Panel from the QList (which is private) I create a getter method that return a pointer to a Panel:

    Panel *getPanel(int index);

    If I use this method in the script the QScriptValue returned doesn't have the right type.
    The return value is a variant.

    How can I convert this variant (variant(void*, ...)) and change its type to Panel (in the script) ?

    Best,

    Angelo

Similar Threads

  1. How to use PERL script in Qt
    By joseph in forum Qt Programming
    Replies: 1
    Last Post: 9th August 2007, 21:57
  2. need script to change qmake .pro file
    By rajeshs in forum Qt Programming
    Replies: 5
    Last Post: 7th July 2007, 17:53
  3. How to Execute Qt script from Qt
    By vishal.chauhan in forum Qt Programming
    Replies: 1
    Last Post: 7th May 2007, 09:12
  4. How to use shell script files ?
    By npc in forum Newbie
    Replies: 3
    Last Post: 15th February 2007, 08:26
  5. testing and testcases
    By sreedhar in forum Qt Programming
    Replies: 3
    Last Post: 7th June 2006, 15:26

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.