Results 1 to 4 of 4

Thread: How to pass a QML ListModel to C++ function

  1. #1
    Join Date
    Jun 2012
    Posts
    18
    Thanked 9 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to pass a QML ListModel to C++ function

    I have a ListModel in QML:

    Qt Code:
    1. ListModel{
    2. id: data
    3. ListElement
    4. {
    5. x: 10
    6. y: 20
    7. count: 18
    8. }
    9. }
    10.  
    11. function draw(){
    12. ctx.drawIntensityMap(data);
    13. }
    14.  
    15.  
    16. //********** c++ code
    17.  
    18. void Context2D::drawIntensityMap(QVariantList data)
    19. {
    20. ....
    21. }
    To copy to clipboard, switch view to plain text mode 

    The model just gets populated with x,y,count data via javascript. After it gets populated I want to pass it to my c++ function so I can draw a HeatMap with QPainter.
    I can't figure out how to pass the data to my C++ function.

    Would I have to pass it as a QVariantList? If so how would I get to the data in C++.

    Thanks

  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: How to pass a QML ListModel to C++ function

    Usually you'd do it the other way -- expose a C++ model that can be populated from QML and then use it from C++.
    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
    Dec 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to pass a QML ListModel to C++ function

    Same question here. Anyone knows or is it possible?

    Quote Originally Posted by wysota View Post
    Usually you'd do it the other way -- expose a C++ model that can be populated from QML and then use it from C++.
    Not an answer

  4. #4
    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 pass a QML ListModel to C++ function

    Well, as wysota said, it is easier on the C++ side to work with concrete types, but if you are content working with the common model interface then that is of course possible as well.

    The ListModel is likely (could be checked in the sources) implemented as a QAbstractListModel subclass.
    So passing the model as a QObject* and casting to that class should work.

    Cheers,
    _

Similar Threads

  1. pass member function as argument int template function
    By bibhukalyana in forum General Programming
    Replies: 1
    Last Post: 12th March 2013, 07:05
  2. How to pass an argument to evaluateJavaScript function?
    By TheIndependentAquarius in forum Qt Programming
    Replies: 5
    Last Post: 5th January 2013, 09:45
  3. Pass struct array to function
    By willbeas_SMU in forum General Programming
    Replies: 2
    Last Post: 19th March 2012, 17:05
  4. Pass data from QT to javascript function
    By thanhluanbk88 in forum Qt Programming
    Replies: 3
    Last Post: 3rd March 2011, 10:40
  5. How to pass a QComboBox to a function?
    By Ricardo_arg in forum General Programming
    Replies: 4
    Last Post: 9th March 2008, 22:16

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.