September 5, 2010

yan yan
Lab Rat
20 posts

Create a global object

 

Hi would like create a Global object to expose function.

And is it possible to add it in a plugin?

thanks

[edit: fixed typos / $chetankjain ]

 Signature 

“QExtend”:http://www.developpez.net/forums/d906429/c-cpp/bibliotheques/qt/naissance-projet-qextend/
“QEXtend Video”:http://www.youtube.com/user/QExtend

12 replies

September 5, 2010

Ivan Čukić Ivan Čukić
Lab Rat
120 posts

If I understood you correctly, you can create a class with a static accessor. Something like:

  1. class Global {
  2.     static Global * self();
  3.  
  4. private:
  5.     Global();
  6.     static Global * instance;
  7.  
  8. };

As for the plugin, you can just modify the accessor to get the object from the plugin.

 Signature 

Ivan Čukić | ivan.cukic(at)kde.org | KDE e.V.
Qt Ambassador

September 5, 2010

yan yan
Lab Rat
20 posts

I want make a global object like Qt Object to expose functions :
http://doc.trolltech.com/main-snapshot/qml-qt.html [doc.trolltech.com]

Or something like

  1. import "file.js" as myObject

And this global Object is added with import of my QML plugin. This object could be coded in C++ or JS

[edit: fixed hyperlink / $chetankjain ]

 Signature 

“QExtend”:http://www.developpez.net/forums/d906429/c-cpp/bibliotheques/qt/naissance-projet-qextend/
“QEXtend Video”:http://www.youtube.com/user/QExtend

September 5, 2010

Ivan Čukić Ivan Čukić
Lab Rat
120 posts

Sorry, didn’t realize you are talking about scripting. Haven’t played with QML that much, bit if things are inherited from the QtScript module, it should go something like this:

  1. engine.globalObject().setProperty("myObject", engine.newQObject(object));

After that, your object will be available to qtscript.

As for making it pluggable with both c++ and js, I can’t really help. (I could think of a dirty way to achieve it, but IMO it is better to wait for our friendly Trolls to tell us the proper way)

 Signature 

Ivan Čukić | ivan.cukic(at)kde.org | KDE e.V.
Qt Ambassador

September 5, 2010

yan yan
Lab Rat
20 posts

Thanks.

They do it in Qt Declarative code (QDeclarativeScriptEngine constructor in QDeclarativeEngine code)

I’ve an access to QDeclarativeEngine which use a QScriptEngine (internaly) on plugin init function.
But i don’t find how to access to a QScriptEngine.

 Signature 

“QExtend”:http://www.developpez.net/forums/d906429/c-cpp/bibliotheques/qt/naissance-projet-qextend/
“QEXtend Video”:http://www.youtube.com/user/QExtend

September 6, 2010

mbrasser mbrasser
Ant Farmer
452 posts

You could try using setContextProperty() on the root context of the engine — this will effectively add the object as a context property that is available to all the elements in the engine. From a plugin, you can do so inside the initializeEngine() [doc.trolltech.com] function.

I believe there are plans to look at this more for a future release, so plugins can easily add “namespaces” like the Qt object with functions, enum values, etc.

September 6, 2010

yan yan
Lab Rat
20 posts

With this method, how create QScriptValue with newXXXX function ?
http://doc.qt.nokia.com/4.7-snapshot/qscriptengine.html [doc.qt.nokia.com]

Another solution that i will test, it’s create a component with QDeclarativeComponent
http://doc.qt.nokia.com/4.7-snapshot/qdeclarativecomponent.html [doc.qt.nokia.com]

Make

  1. import "file.js" as myObject
equivalent in qmldir file could be interesting.

[edit: fixed hyperlink / $chetakjain ]

 Signature 

“QExtend”:http://www.developpez.net/forums/d906429/c-cpp/bibliotheques/qt/naissance-projet-qextend/
“QEXtend Video”:http://www.youtube.com/user/QExtend

September 6, 2010

Deleted Member # 4a2 Deleted Member # 4a2
Ant Farmer
1481 posts

hi yan, you could also use the link feature for hyperlinks

September 6, 2010

yan yan
Lab Rat
20 posts

chetankjain wrote:
hi yan, you could also use the link feature for hyperlinks

Sorry. I don’t see this forum don’t make auto-linked.

 Signature 

“QExtend”:http://www.developpez.net/forums/d906429/c-cpp/bibliotheques/qt/naissance-projet-qextend/
“QEXtend Video”:http://www.youtube.com/user/QExtend

September 6, 2010

Deleted Member # 4a2 Deleted Member # 4a2
Ant Farmer
1481 posts

yan wrote:

Sorry. I don’t see this forum don’t make auto-linked.

That I believe is in the roadmap … :)

September 7, 2010

mbrasser mbrasser
Ant Farmer
452 posts
yan_ wrote:
With this method, how create QScriptValue with newXXXX function ?

With the setContextProperty() approach, you would use a QObject with slots or Q_INVOKABLEs for your “global” object, rather than a QScriptValue. The Minehunt demo shows this approach (adds a MinehuntGame object in the root context).

September 7, 2010

yan yan
Lab Rat
20 posts

mbrasser wrote:
With the setContextProperty() approach, you would use a QObject with slots or Q_INVOKABLEs for your “global” object, rather than a QScriptValue. The Minehunt demo shows this approach (adds a MinehuntGame object in the root context).

Thanks.
I’ve ever test it and it’s work perfectly ^^

I find a solution for JS code :D
1- create a qml file wich define function on the root element (I use QtObject)
2- in initializeEngine(), use a“QDeclarativeComponent “:http://doc.qt.nokia.com/4.7-snapshot/qdeclarativecomponent.html.
3- set qml file path or use setData
4- use create to make a QObject
5- add this QObject with setContextProperty

Like QDeclarativeComponent use qml file, i think it’s so possible to mix JS and C++ code

 Signature 

“QExtend”:http://www.developpez.net/forums/d906429/c-cpp/bibliotheques/qt/naissance-projet-qextend/
“QEXtend Video”:http://www.youtube.com/user/QExtend

September 8, 2010

johnlamericain johnlamerica..
Lab Rat
3 posts

It’s really impressive, thanks for that !

 
  ‹‹ [Solved] C++ Signals for QML Layer      IcecastRadio - Qt-Widget/Qt-Quick example Icecast player ››

You must log in to post a reply. Not a member yet? Register here!