Results 1 to 17 of 17

Thread: How to access any method of any qml item from c++ side

  1. #1
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default How to access any method of any qml item from c++ side

    hi forum.my qml:
    Qt Code:
    1. ListModel {
    2. id: list
    3. objectName:"list"
    4. }
    To copy to clipboard, switch view to plain text mode 
    my cpp:
    Qt Code:
    1. int main( int argc, char ** argv )
    2. {
    3. QGuiApplication app(argc,argv);
    4. QQmlApplicationEngine* engine = new QQmlApplicationEngine();
    5. engine->load(QUrl::fromLocalFile("main.qml"));
    6.  
    7. QObject* root = engine->rootObjects()[0];
    8.  
    9. QObject* list = root->findChild<QObject*>("list");
    10.  
    11. QString qq("list.append({\"file\":\"/home/pc/afile\", \"mode\":660})");
    12.  
    13. QQmlExpression zz(engine->rootContext(), root, qq);
    14.  
    15. zz.evaluate();
    16.  
    17. if(zz.hasError())qDebug()<<"No luck";
    18.  
    19. return app.exec();
    20. }
    To copy to clipboard, switch view to plain text mode 

    Above approach didnt work.i already tried 3-4 diffrerent approach.but no luck. always i get "No luck"

    BTW: FileDialog from QtQuick.Dialogs didnt work for me.
    Qt Code:
    1. import QtQuick 2.3
    2. import QtQuick.Dialogs 1.1
    3.  
    4. FileDialog {
    5. title: "Please choose a file"
    6. Component.onCompleted: visible = true
    7. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. [pc@pc ~]$ qmlscene-qt5 above.qml
    2. QInotifyFileSystemWatcherEngine::addPaths: inotify_add_watch failed: No such file or directory
    3. QObject::connect: No such signal org::freedesktop::UPower::DeviceAdded(QString)
    4. QObject::connect: No such signal org::freedesktop::UPower::DeviceRemoved(QString)
    5. "URL cannot be listed
    6. trash:/"
    7. "URL cannot be listed
    8. trash:/"
    9. KSambaShare: Could not find smb.conf!
    10. [pc@pc ~]$
    To copy to clipboard, switch view to plain text mode 

    anyone can help me a bit. i am using 64bit arch-linux with updated.Thanks.

  2. #2
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default how to access a methot of any qml item from c++

    hi forum.i already access QQuickItem methots.But in case below i tried 3-4 diffrent approach.i cant succeed.Help me a bit please.
    Qt Code:
    1. //snipped
    2. ListModel {
    3. id: list
    4. objectName:"list"
    5. }
    6. //snipped
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. int main( int argc, char ** argv )
    2. {
    3. QGuiApplication app(argc,argv);
    4. QQmlApplicationEngine* engine = new QQmlApplicationEngine();
    5. engine->load(QUrl::fromLocalFile("above.qml"));
    6. QObject* root = engine->rootObjects()[0]; //only one root object exist in my case
    7. QObject* list = root->findChild<QObject*>("list");
    8. QString qq("list.append({\"file\":\"/home/pc/afile\",\"mode\":660})");
    9. QQmlExpression zz(engine->rootContext(), root,qq);
    10. zz.evaluate();
    11. if(zz.hasError())qDebug()<<"No luck";
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by luigide; 17th September 2014 at 16:03.

  3. #3
    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: how to access a methot of any qml item from c++

    I guess this is a case where it would really help to know what actual problem you are trying to solve.
    The example does not make any sense for a real project context, as one wouldn't create an empty model in QML and then fill it from C++.

    Cheers,
    _

  4. #4
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: how to access a methot of any qml item from c++

    i have a TableView on qml side. i want to fill it using ListModel.in Fact i am try to make a file search application (like kfind from kde ) based on QtQuick.

    Search related works left on c++ side. and gui qml side.Now i am try to provide connection between them.Thanks for reply

    in fact i am able to find a workaround less aesthetics
    Last edited by luigide; 17th September 2014 at 15:59.

  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: how to access a methot of any qml item from c++

    So create a model in C++ (based on QAbstractItemModel or its subclass) and export it to QML where you will attach it to a QtQuick ListView.
    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.


  6. The following user says thank you to wysota for this useful post:

    luigide (17th September 2014)

  7. #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: how to access a methot of any qml item from c++

    In such a case you would usually have the model in C++ and export it to QML and use it there.

    A list model is very easy to create. Basically just derive from QAbstractListModel and implement rowCount() and data().
    And optionally overwrite roleNames() if you want more roles accessible from QML.

    Cheers,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    luigide (17th September 2014)

  9. #7
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: how to access a methot of any qml item from c++

    So i will go that you advised.Thanks both.

  10. #8
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: how to access a methot of any qml item from c++

    There is no need to create your own custom class for invoke any method of any qml object from c++ side. As:
    Qt Code:
    1. snipped
    2.  
    3. QObject* list = root->findChild<QObject*>("list");
    4.  
    5. int append = list->metaObject()->indexOfMethod("append(jsobject)");
    6.  
    7. QMetaMethod method = list->metaObject()->method(append);
    8.  
    9. method.invoke(list,Q_ARG(blah blah),Q_ARG(blah blah), ...);
    10.  
    11. snipped
    To copy to clipboard, switch view to plain text mode 

    Works perfectly.

  11. #9
    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: how to access a methot of any qml item from c++

    Yes, but for the use case of the thread starter still the wrong solution.

    Cheers,
    _

  12. #10
    Join Date
    Oct 2015
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: how to access a methot of any qml item from c++

    I'm trying to do the same thing and it does not work for me. My test code does not find the "append(jsobject)" method, in my case the append method is show as "append(QQmlV4Function*)" and the QQmlV4Function type is a private Qt type so I can't invoke the method. Can provide more details on how you implemented this? What arguments are you passing on Q_ARG(blah blah)? What version of Qt are you using? Thanks.

  13. #11
    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: how to access a methot of any qml item from c++

    Again: create the model in C++, export an instance of it to QML via setContextProperty.

    Cheers,
    _

  14. #12
    Join Date
    Oct 2015
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: how to access a methot of any qml item from c++

    Quote Originally Posted by anda_skoa View Post
    Again: create the model in C++, export an instance of it to QML via setContextProperty.

    Cheers,
    _
    That's actually not an option for me or at least I can't think of a way to make it work for me. I'm working on an interface to a graphical programming abstraction layer for QML so the user will create QML GUIs and then manipulate their properties with the graphical programming interface. My problem is that I have to manipulate lists created in QML with roles and data that is only available at run time. I tried adding dynamic properties to objects but it seems they have to be declared with Q_PROPERTY at compile time to be usable with the setContextProperty method. I actually managed to manipulate lists using QQmlExpression but the QMetaMethod's invoke method approach seems more elegant to me.

  15. #13
    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: how to access a methot of any qml item from c++

    So the user uses your graphic abstraction for the UI but then types ListModels manually?
    Wouldn't it be better to allow the user to edit model data with some nice editor UI as well?

    Otherwise what do you gain over using the QtQuick designer?

    Cheers,
    _

  16. #14
    Join Date
    Oct 2015
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: how to access a methot of any qml item from c++

    Quote Originally Posted by anda_skoa View Post
    So the user uses your graphic abstraction for the UI but then types ListModels manually?
    Wouldn't it be better to allow the user to edit model data with some nice editor UI as well?

    Otherwise what do you gain over using the QtQuick designer?

    Cheers,
    _
    Not exactly, the users will design the QML UI on the designer or by hand. All the logic will be done in a graphics based code simulation environment that will interact with the QML UI using the library that I'm putting together. They will have to create a ListModel manually and then manipulate the elements using the graphical blocks from the code simulation program. Then they can generate code from the simulation program, compile it and have a fully functioning application with minimal manual text coding.

  17. #15
    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: how to access a methot of any qml item from c++

    So do they add ListElement entries manually or just create a ListModel?

    Cheers,
    _

  18. #16
    Join Date
    Oct 2015
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: how to access a methot of any qml item from c++

    Quote Originally Posted by anda_skoa View Post
    So do they add ListElement entries manually or just create a ListModel?

    Cheers,
    _
    They create the initial ListModel and ListElements in QML and then add, delete and modify the elements from the graphical code simulation environment.

  19. #17
    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: how to access a methot of any qml item from c++

    Ah, hmm.

    Without the need to add elements in QML code that would be easy: a custom model type would allow instantiation from QML and also provide complete control over its C++ API.

    Ok, so you'll need a ListModel subtype that has additional functions.
    Something like
    Qt Code:
    1. // MyListModel
    2. import QtQuick 2.0
    3.  
    4. ListModel {
    5. function appendFromJSON(string jsonString) {
    6. // get data from jsonString, call append()
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    Then use MyListModel instead of ListModel.

    Cheers,
    _

    P.S.: be sure you and the users understand the inherent limitations of ListModel/ListElement, e.g. can't deal with translatable strings.
    This is usually only used for prototyping while real model data is not available or for very rare cases when data is fixed and does not contain user visible text.

Similar Threads

  1. How to call a static method of java from c++ side?
    By stereoMatching in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 31st December 2013, 16:54
  2. Replies: 0
    Last Post: 15th December 2011, 06:54
  3. Replies: 3
    Last Post: 15th April 2010, 14:26
  4. How to access myApp->method from a child
    By bnilsson in forum Qt Programming
    Replies: 2
    Last Post: 13th July 2008, 14:22

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.