Results 1 to 6 of 6

Thread: call Javascript from C++ method with QQuickItem parameter

  1. #1
    Join Date
    Jan 2015
    Posts
    6
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default call Javascript from C++ method with QQuickItem parameter

    Hello,

    we have an application that heavily integrates C++ and QML. Now, the need arises to call a javascript method defined on a QML component with a parameter that is a QQuickItem *. Here is the relevant QML code:
    Qt Code:
    1. Rectangle {
    2. function show(targetItem) {
    3. _internal_stackView.push({item:targetItem,destroyOnPop:false})
    4. }
    5. }
    To copy to clipboard, switch view to plain text mode 
    from C++, I would like to do this:
    Qt Code:
    1. QMetaObject::invokeMethod(m_qmlItem, "show", Q_RETURN_ARG(QVariant, retval), Q_ARG(QQuickItem *, nextItemToShow));
    To copy to clipboard, switch view to plain text mode 
    but alas, there seems to be no way to pass anything but primitve values (those supported by QVariant) as method parameter. The code compiles, but upon execution there is a message in the console that the parameter is inacceptable. I have also not found any other way to pass the "nextItemToShow" (context property doesn't work because context not writeable, QML property has the same restrictions as QML method)

    Any suggestions?
    thanks,
    Chris

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: call Javascript from C++ method with QQuickItem parameter

    Aside from the bad idea to call a QML method from C++, have you tried QObject* as the type, or putting the item into a QVariant first?

    Cheers,
    _

  3. #3
    Join Date
    Jan 2015
    Posts
    6
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: call Javascript from C++ method with QQuickItem parameter

    Aside from the bad idea to call a QML method from C++
    I wouldn't be so final about this, as it is an officially documented technique. Beyond that, our use case is a generator based on a higher-level DSL - not the typical QML application.

    have you tried QObject* as the type, or putting the item into a QVariant first?
    the runtime error message states that the JS method expects a QVariant, and putting the item into a QVariant is not possible, as I wrote in my first post

    thanks, C.

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

    Default Re: call Javascript from C++ method with QQuickItem parameter

    Quote Originally Posted by gsv-chris View Post
    I wouldn't be so final about this, as it is an officially documented technique.
    And as far as I remember this is officially said to be a bad idea

    and putting the item into a QVariant is not possible, as I wrote in my first post
    Why not? I don't think the problem lies with putting a QObject in a QVariant.

    This works just fine:

    Qt Code:
    1. #include <QtGui>
    2. #include <QQuickView>
    3. #include <QQuickItem>
    4.  
    5. int main(int argc, char **argv) {
    6. QGuiApplication app(argc, argv);
    7. QQuickView view;
    8. view.setSource(QUrl::fromLocalFile("main.qml"));
    9. view.show();
    10. app.processEvents();
    11. QObject *rootObject = view.rootObject();
    12. v.setValue<QObject*>(rootObject);
    13. QMetaObject::invokeMethod(rootObject, "abc", Q_ARG(QVariant, v));
    14. return 0;
    15. }
    To copy to clipboard, switch view to plain text mode 

    javascript Code:
    1. import QtQuick 2.0
    2.  
    3. Rectangle {
    4. function abc(o) { console.log("I'm here") }
    5. }
    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
    Jan 2015
    Posts
    6
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: call Javascript from C++ method with QQuickItem parameter

    Why not? I don't think the problem lies with putting a QObject in a QVariant.
    perfect, thats the answer I was looking for. Thanks alot!

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: call Javascript from C++ method with QQuickItem parameter

    Quote Originally Posted by gsv-chris View Post
    the runtime error message states that the JS method expects a QVariant, and putting the item into a QVariant is not possible, as I wrote in my first post
    No, you did not write anything like that in your first post.
    And yes, it is possible.

    Cheers,
    _

Similar Threads

  1. Call JavaScript function with parameters from Qt C++
    By qt_developer in forum Qt Programming
    Replies: 5
    Last Post: 13th January 2015, 00:31
  2. Replies: 3
    Last Post: 12th November 2014, 08:38
  3. Call a JavaScript function from C++ using QtWebkit
    By Rastersoft in forum Qt Programming
    Replies: 9
    Last Post: 14th May 2014, 12:03
  4. Function call with QFile as parameter?
    By Pyry in forum Newbie
    Replies: 2
    Last Post: 26th May 2011, 00:10
  5. Replies: 0
    Last Post: 20th December 2009, 15:37

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.