Results 1 to 8 of 8

Thread: How to push values to QML property variant two dimensional array - dynamically?

  1. #1
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default How to push values to QML property variant two dimensional array - dynamically?

    This is what I have tried:
    Qt Code:
    1. import QtQuick 2.0
    2.  
    3. Rectangle
    4. {
    5. property variant twoDimTempArray: [[]]
    6. property variant oneDArray: [1,2,3]
    7.  
    8. MouseArea
    9. {
    10. anchors.fill: parent
    11. onClicked:
    12. {
    13. twoDimTempArray.push (oneDArray)
    14.  
    15. twoDimTempArray[0].push (oneDArray)
    16.  
    17. twoDimTempArray[0][0] = oneDArray[0]
    18.  
    19. console.log (twoDimTempArray)
    20. }
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

    They all results in
    Qt Code:
    1. []
    To copy to clipboard, switch view to plain text mode 
    .

    How to push values in QML property variant two dimensional array?

  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: How to push values to QML property variant two dimensional array - dynamically?

    What exactly are you trying to do?

    Cheers,
    _

  3. #3
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: How to push values to QML property variant two dimensional array - dynamically?

    I want to pass this 2 dimentional array from QML to C++ after filling some dynamically calculated values in it.

  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 push values to QML property variant two dimensional array - dynamically?

    Why don't you create and calculate in C++ directly?

    It seems you are trying to solve a problem that doesn't exist in a properly designed program.

    Can you give a real world example instead of an abstract explanation?

    Cheers,
    _

  5. #5
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: How to push values to QML property variant two dimensional array - dynamically?

    Quote Originally Posted by anda_skoa View Post
    Why don't you create and calculate in C++ directly?
    This question does not make much sense to me.

    I have a GUI designed in QML. User is supposed to fill in some values there.
    I am supposed to calculate something from those values and then pass them to C++ so that they can be used
    in an algorithm.

    You think this kind of problem cannot exist in a properly designed program?

    Anyways,

    I have found a temporary solution as follows:

    One way to add the values dynamically to a 1 dimensional QML variant is to fill a normal Javascript array and then assign it to the QML variant.
    Qt Code:
    1. import QtQuick 2.0
    2.  
    3. Rectangle
    4. {
    5. property variant oneDArray: []
    6. MouseArea
    7. {
    8. anchors.fill: parent
    9. onClicked:
    10. {
    11. var t = new Array (0)
    12. t.push(11)
    13. t.push(12)
    14.  
    15. oneDArray = t
    16.  
    17. console.log (oneDArray)
    18. }
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    Output:
    Qt Code:
    1. Starting /home/.../documents/test/build-junk-Desktop_Qt_5_1_0_GCC_64bit-Debug/junk...
    2. QML debugging is enabled. Only use this in a safe environment.
    3. [11,12]
    4. /home/.../documents/test/build-junk-Desktop_Qt_5_1_0_GCC_64bit-Debug/junk exited with code 0
    To copy to clipboard, switch view to plain text mode 

  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: How to push values to QML property variant two dimensional array - dynamically?

    Quote Originally Posted by TheIndependentAquarius View Post
    This question does not make much sense to me.
    The question if you can provide a real world example?

    Quote Originally Posted by TheIndependentAquarius View Post
    I have a GUI designed in QML. User is supposed to fill in some values there.
    So far so goo.

    Quote Originally Posted by TheIndependentAquarius View Post
    I am supposed to calculate something from those values and then pass them to C++ so that they can be used
    in an algorithm.
    Why don't you simply pass the values from the UI?
    What makes it necessary to do calculations in the UI?

    Quote Originally Posted by TheIndependentAquarius View Post
    You think this kind of problem cannot exist in a properly designed program?
    No. But in a lot of cases strange requirements for QML side code are hints that the program does things in the UI context that would better be done in the application's non-UI parts.

    Quote Originally Posted by TheIndependentAquarius View Post
    One way to add the values dynamically to a 1 dimensional QML variant is to fill a normal Javascript array and then assign it to the QML variant.
    Which also has the advantage of changing the value, meaning property bindings get updated.

    Cheers,
    _

  7. #7
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: How to push values to QML property variant two dimensional array - dynamically?

    Quote Originally Posted by anda_skoa View Post
    Why don't you simply pass the values from the UI?
    I have several points on the UI. I want the user to select some points from them.
    I need to calculate the distance between the points selected by the user.
    This part can be done in C++ for sure,

    but I still will have to `push` the points selected by the user in a `variant` and then pass the variant to C++.
    Won't I?

  8. #8
    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 push values to QML property variant two dimensional array - dynamically?

    Depends on how you get these points in the first place.

    For example if they originate in C++, e.g. as data accessed through a model, then selection could also be tracked there (state less UI).
    Or the selection could be tracked in QML and passed to C++ when done.

    But yes, if the values originate in QML then you'll need to pass them to C++.
    Either using a C++ based property or a function call. The latter could take a list or each point individually.

    Which is why I was asking for the actual use case really early.

    It is usually better to find a solution to the actual problem and not to some abstract, academic one

    Cheers,
    _

Similar Threads

  1. Q_ENUMS as property variant index
    By oosavu in forum Qt Quick
    Replies: 0
    Last Post: 9th April 2013, 06:07
  2. 2 Dimensional array in QML
    By nestuser in forum Qt Programming
    Replies: 3
    Last Post: 5th October 2012, 07:38
  3. Operations in 2 dimensional array
    By Stanfillirenfro in forum Qt Programming
    Replies: 4
    Last Post: 5th August 2012, 16:50
  4. two dimensional array, size determined dynamically
    By feraudyh in forum General Programming
    Replies: 9
    Last Post: 10th June 2010, 03:00
  5. use QVector as 2 dimensional Array
    By umulingu in forum Qt Programming
    Replies: 3
    Last Post: 1st January 2010, 12:31

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.