Results 1 to 6 of 6

Thread: How to pass a 2 dimensional float array from QML to C++?

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

    Default How to pass a 2 dimensional float array from QML to C++?

    I have a two dimensional float array in QML. How do I get its values in C++.

    I have created a class in c++ and have done the part of qmlRegisterType. The class is now accessible in QML.

    Please demonstrate with a small example.

    This is what I have tried:

    Qt Code:
    1. #ifndef TEST_H
    2. #define TEST_H
    3.  
    4. #include <QObject>
    5. #include <QQuickItem>
    6. #include <iostream>
    7.  
    8. class Test: public QObject
    9. {
    10. private:
    11. Q_OBJECT
    12.  
    13. Q_PROPERTY(QVariantList name READ name WRITE setName)
    14. QVariantList m_name;
    15.  
    16. public:
    17. QVariantList name() const
    18. {
    19. return m_name;
    20. }
    21.  
    22. public slots:
    23. void setName (QVariantList arg)
    24. {
    25. m_name = arg;
    26.  
    27. QVector<QVariant> p = m_name.toVector ();
    28. std::cout << p.first ().toInt ();
    29. }
    30. };
    31.  
    32. #endif // TEST_H
    To copy to clipboard, switch view to plain text mode 

    The c++ part is giving me a segmentation fault.

    QML:
    Qt Code:
    1. import QtQuick 2.2
    2. import QtQuick.Window 2.1
    3.  
    4. import AGVUI 1.0
    5.  
    6. Window {
    7. visible: true
    8. width: 360
    9. height: 360
    10.  
    11. function createTwoDimensionalArray (twoDimArray, rows, cols)
    12. {
    13. /// Creates all lines.
    14. for (var i = 0; i < rows; i++)
    15. {
    16. /// Creates an empty line.
    17. twoDimArray.push ([]);
    18.  
    19. /// Ads colunms to the empty line.
    20. twoDimArray [i].push (new Array (cols));
    21.  
    22. for (var j = 0; j < cols; j++)
    23. {
    24. /// Default initialization to zero
    25. twoDimArray [i][j] = 0
    26. }
    27. }
    28.  
    29. return twoDimArray
    30. }
    31.  
    32. Test
    33. {
    34. Component.onCompleted:
    35. {
    36. var arr = []
    37. createTwoDimensionalArray (arr, 2, 2)
    38.  
    39. setName(arr)
    40. }
    41. }
    42.  
    43. MouseArea {
    44. anchors.fill: parent
    45. onClicked: {
    46. Qt.quit();
    47. }
    48. }
    49.  
    50. Text {
    51. text: qsTr("Hello World")
    52. anchors.centerIn: parent
    53. }
    54. }
    To copy to clipboard, switch view to plain text mode 

  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 pass a 2 dimensional float array from QML to C++?

    What did you try to debug it so far?

    What did the stack trace of the crash tell you?

    Have checked the size of the "arg" list? Does the list contain at least one element?

    Cheers,
    _

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

    Default Re: How to pass a 2 dimensional float array from QML to C++?

    Here's what I have tried:

    Header:
    Qt Code:
    1. #include <QQuickItem>
    2. #include <iostream>
    3.  
    4. class Controller : public QObject
    5. {
    6. Q_OBJECT
    7.  
    8. Q_PROPERTY(QList <QVariantList> names READ names WRITE setnames NOTIFY namesChanged)
    9. QList <QVariantList> m_names;
    10.  
    11. public:
    12. Controller()
    13. {
    14. }
    15. ~Controller() {
    16. }
    17.  
    18. QList <QVariantList> names() const
    19. {
    20. return m_names;
    21. }
    22.  
    23. public slots:
    24. void setnames(QList <QVariantList> arg)
    25. {
    26. QVariantList p;
    27. if (arg.size () > 0)
    28. {
    29. p = arg.first ();
    30. std::cout << "\narg: \n" << p[0].toInt ();
    31. }
    32. else
    33. std::cout << "\nqqqq " << arg.size () << "\n";
    34. }
    35.  
    36. signals:
    37. void namesChanged(QList <QVariantList> arg);
    38. };
    To copy to clipboard, switch view to plain text mode 

    qml

    Qt Code:
    1. import QtQuick 2.0
    2. import FromCpp 1.0
    3.  
    4. Rectangle
    5. {
    6. property variant arras: [[1,2,3], [4,5,6]]
    7. Controller
    8. {
    9. id: ppp
    10. }
    11.  
    12. MouseArea
    13. {
    14. anchors.fill: parent
    15. onClicked:
    16. {
    17. ppp.setnames(arras)
    18. console.log(arras.length)
    19. }
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 


    The exact output from QtCreator:
    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. 2
    4.  
    5. qqqq 0
    6. QThreadStorage: Thread 0x181e270 exited after QThreadStorage 2 destroyed
    7. /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 
    ///

    Here, as you can see the size of the 2D array from QML is printed correctly as 2 and the size is printed 0 from c++.

    Why is that happening? Please explain.

  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: How to pass a 2 dimensional float array from QML to C++?

    Where is the segmentation fault? What is the backtrace for it? Why did you choose QList<QVariantList> as the argument type? Why not use QVariant instead? What is the effect you wish to obtain? Maybe a QQmlListProperty would be a better solution?
    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. The following user says thank you to wysota for this useful post:

    TheIndependentAquarius (29th September 2014)

  6. #5
    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 2 dimensional float array from QML to C++?

    It is strange that the console.log output would happen before the C++ log output despite it being after the call to the slot.

    Have you tried a one dimensional array?
    What do you get as C++ output when you have your orignal argument type, i.e. QVariantList?

    Cheers,
    _

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

    TheIndependentAquarius (29th September 2014)

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

    Default Re: How to pass a 2 dimensional float array from QML to C++?

    Thanks to both of you for your time.

    In the second attempt of mine, I didn't get a segmenation fault.
    When I tried to print the size of the argument, it printed 0. That was the problem.

    From another QA site I came to know that `QVariantList` as an argument is sufficient.
    What I didn't know was that to access each row we first need to convert it to a list.

    The following works now:
    Qt Code:
    1. void setname (QVariantList arg)
    2. {
    3. if (arg.size())
    4. {
    5. QList <QVariant> p = arg[0].toList();
    6.  
    7. std::cout << "\nRow0 0:" << p[0].toInt ();
    8. std::cout << "\nRow0 1:" << p[1].toInt ();
    9. std::cout << "\nRow0 2:" << p[2].toInt ();
    10.  
    11. std::cout << "\n";
    12.  
    13. QList <QVariant> p1 = arg[1].toList();
    14.  
    15. std::cout << "\nRow1 0:" << p1[0].toInt ();
    16. std::cout << "\nRow1 1:" << p1[1].toInt ();
    17. std::cout << "\nRow1 2:" << p1[2].toInt ();
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by anda_skoa View Post
    It is strange that the console.log output would happen before the C++ log output despite it being after the call to the slot.
    This is something that is still occurring and I don't know what causes it.

Similar Threads

  1. 2 Dimensional array in QML
    By nestuser in forum Qt Programming
    Replies: 3
    Last Post: 5th October 2012, 07:38
  2. Operations in 2 dimensional array
    By Stanfillirenfro in forum Qt Programming
    Replies: 4
    Last Post: 5th August 2012, 16:50
  3. Replies: 2
    Last Post: 18th December 2011, 23:34
  4. Multi-dimensional objects array
    By matulik in forum Newbie
    Replies: 3
    Last Post: 23rd October 2011, 02:20
  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
  •  
Qt is a trademark of The Qt Company.