Results 1 to 4 of 4

Thread: Most efficient way to implement data in QML from C++

  1. #1
    Join Date
    Nov 2016
    Location
    Minneapolis, MN
    Posts
    2
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Most efficient way to implement data in QML from C++

    QUESTION: Given the platform and amount of data - what is the best method for populating model in QML that allows for the usage of delegates

    I spent a couple of days researching this but have not found an answer to this.
    BACKGROUND:

    I'm starting a Qt project that will use three primary tables (each table has approx 23 rows, 10 columns - approx 230 data points for each table (~750 data points total).

    Need direction on the best possible way to build and update these tables in c++ while allowing the usage of delegates in QML

    The tables will NOT grow or shrink in size at run time (i.e. add or remove rows or columns ) but the data will be updated so any values that are painted on the screen must be updated

    The 800 values (spanning 3 tables) are currently stored and manipulated in a seperate C files that uses static arrays with "get" and "set" functions

    The two ways we are currently looking at are:
    a) creating a list model in QML and populating data with javascript (see code example below
    This method is working but we are concerned about performance issues using javascript over c++
    QUESTION: Is this the best method?

    b) creating a QAbstractTableModel in c++
    new to this concept and am concerned that I will have to create individual get / set functions for each column. We currently have over 50 columns. (I believe these are "roles" within the QAbstractmodel )
    QUESTION: Does QAbstractTableModel allow for the usage of delegates in QML, If using QAbstractmodel is the best approach, then does every column need it's own get set function?


    QUESTION: Is there a third option that we are not considering?


    Platform will be on an embedded system using the TI AM335 driving a 800x480 LCD (same as the beaglebone Black) and embedded linux

    Here is an example of option A
    Qt Code:
    1. //create instance of c++ class
    2. CrcDataQML2{
    3. id: crcClassInstance
    4. }
    5.  
    6.  
    7.  
    8. //create List Model
    9. ListModel{
    10. id: crcPointListModel
    11. }
    12.  
    13.  
    14. function setIndividualValue(columnToSet, IndexToSet, valueToSet)
    15. {
    16. crcClassInstance.setDataQML(columnToSet, IndexToSet, valueToSet);
    17. }
    18.  
    19.  
    20. //function to populate values in ListModel
    21. function getDataValues(id)
    22. {
    23. var i;
    24. var j;
    25. var k;
    26. console.log()
    27. for (i = 0; i<23; i++ )
    28. {
    29.  
    30. id.set(i,{ "setPoint":crcClassInstance.getDataQML(Global.POINT_SET_POINT,i),
    31. "currentValue":crcClassInstance.getDataQML(Global.POINT_CURRENT_VALUE,i),
    32. "engineeringUnits":crcClassInstance.getDataQML(Global.POINT_ENGINEERING_UNITS,i),
    33. "resolution":crcClassInstance.getDataQML(Global.POINT_RESOLUTION,i),
    34.  
    35. name:crcClassInstance.getQMLDataString(Global.POINT_NAME,i),
    36. "currentValueSource":crcClassInstance.getDataQML(Global.POINT_CV_ORIGIN,i),
    37. "setPointSource":crcClassInstance.getDataQML(Global.POINT_SP_ORIGIN,i),
    38. "alarmHigh":crcClassInstance.getDataQML(Global.POINT_ALARM_OVER,i),
    39. "alarmLow":crcClassInstance.getDataQML(Global.POINT_ALARM_UNDER,i),
    40. "alarmStatus":crcClassInstance.getDataQML(Global.POINT_ALARM_STATUS,i),
    41. "displayPoint":crcClassInstance.getBoolDataQML(Global.POINT_DISPLAY,i),
    42. "setPointDisplay":false,
    43. "alarmSetup":3,
    44. "currentValueOffset":crcClassInstance.getDataQML(Global.POINT_CURRENT_VALUE_OFFSET,i)
    45. });
    46. }
    47. }
    48.  
    49.  
    50. Timer{
    51. id: timerSam
    52. running: true
    53. repeat: true
    54. triggeredOnStart: true
    55. interval: 1000
    56. onTriggered: {
    57. //updates values in c++
    58. //getDataValues(crcPointListModel);
    59. testRowInsertion(crcPointListModel,"name");
    60. //sets a value in c++ equal to the current secnd value - just to prove we are able to manipulate values in c++
    61. setIndividualValue(Global.POINT_CURRENT_VALUE, 0, parseInt(Qt.formatTime(new Date(),"ss")) );
    62.  
    63. }
    64. }
    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: Most efficient way to implement data in QML from C++

    Quote Originally Posted by sam_s View Post
    QUESTION: Given the platform and amount of data - what is the best method for populating model in QML that allows for the usage of delegates
    A custom C++ model.

    The delegate part is on the QML side, it doesn't care what kind of model you have. Even a plain number can be treated as a model.

    Quote Originally Posted by sam_s View Post
    The two ways we are currently looking at are:
    a) creating a list model in QML and populating data with javascript (see code example below
    This method is working but we are concerned about performance issues using javascript over c++
    QUESTION: Is this the best method?
    No

    Quote Originally Posted by sam_s View Post
    b) creating a QAbstractTableModel in c++
    new to this concept and am concerned that I will have to create individual get / set functions for each column. We currently have over 50 columns. (I believe these are "roles" within the QAbstractmodel )
    QUESTION: Does QAbstractTableModel allow for the usage of delegates in QML, If using QAbstractmodel is the best approach, then does every column need it's own get set function?
    While you can use QAbstractTableModel, e.g. if you also want to show the data in a widget based QTableView, QtQuick will only treat it as a list model.
    A table model can still work if the model can map the QML roles onto columns, or you just derive from QAbstractListModel.

    Quote Originally Posted by sam_s View Post
    QUESTION: Is there a third option that we are not considering?
    Yes, QAbstractListModel

    Cheers,
    _

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

    sam_s (4th November 2016)

  4. #3
    Join Date
    Nov 2016
    Location
    Minneapolis, MN
    Posts
    2
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Most efficient way to implement data in QML from C++

    Thank you anda_skoa for your help and quick turn around

    I'm still really new to Qt and can't find a decent example on how to populate the QAbstractTableModel with row and column data similar to how I was doing it in QML

    In this QML code I was able to populate each row with the following code

    Qt Code:
    1. ListModel{
    2. id: crcPointListModel
    3. }
    4.  
    5. function getDataValues(id)
    6. {
    7. var i;
    8. var j;
    9.  
    10. for (i = 0; i<23; i++ )
    11. {
    12.  
    13. id.set(i,{ "setPoint":crcClassInstance.getDataQML(Global.POINT_SET_POINT,i),
    14. "currentValue":crcClassInstance.getDataQML(Global.POINT_CURRENT_VALUE,i),
    15. "engineeringUnits":crcClassInstance.getDataQML(Global.POINT_ENGINEERING_UNITS,i),
    16. "resolution":crcClassInstance.getDataQML(Global.POINT_RESOLUTION,i),
    17.  
    18. name:crcClassInstance.getQMLDataString(Global.POINT_NAME,i),
    19. "currentValueSource":crcClassInstance.getDataQML(Global.POINT_CV_ORIGIN,i),
    20. "setPointSource":crcClassInstance.getDataQML(Global.POINT_SP_ORIGIN,i),
    21. "alarmHigh":crcClassInstance.getDataQML(Global.POINT_ALARM_OVER,i),
    22. "alarmLow":crcClassInstance.getDataQML(Global.POINT_ALARM_UNDER,i),
    23. "alarmStatus":crcClassInstance.getDataQML(Global.POINT_ALARM_STATUS,i),
    24. "displayPoint":crcClassInstance.getBoolDataQML(Global.POINT_DISPLAY,i),
    25. "setPointDisplay":false,
    26. "alarmSetup":3,
    27. "currentValueOffset":crcClassInstance.getDataQML(Global.POINT_CURRENT_VALUE_OFFSET,i)
    28. });
    29. }
    30. }
    31.  
    32. Timer{
    33. id: timerSam
    34. running: true
    35. repeat: true
    36. triggeredOnStart: true
    37. interval: 1000
    38. onTriggered: {
    39. //updates values in c++
    40. getDataValues(crcPointListModel);
    41. //sets a value in c++ equal to the current secnd value - just to prove we are able to manipulate values in c++ and see them within the QML delegate
    42. setIndividualValue(Global.POINT_CURRENT_VALUE, 0, parseInt(Qt.formatTime(new Date(),"ss")) );
    43.  
    44. }
    45. }
    To copy to clipboard, switch view to plain text mode 
    Here are my questions:
    - Does the QAbstractListModel have its own built in model / container?
    - If it doesn't have its own container - given the model that this would have to replace in the QML code above - what would be the best container? QVector <QObject> ?
    - If using QAbstractmodel is the best approach, then does every column need it's own get set function or can I populate each row with column / data as I was able to above?

    Any direction or examples would help out greatly.

    Thank you again for your help!

  5. #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: Most efficient way to implement data in QML from C++

    Quote Originally Posted by sam_s View Post
    - Does the QAbstractListModel have its own built in model / container?
    No, it is just an interface to the data.
    Your implementation can hold the data or delegate to a class that holds the data.
    Your "crcClassInstances" sounds like you already have the latter.

    Quote Originally Posted by sam_s View Post
    - If it doesn't have its own container - given the model that this would have to replace in the QML code above - what would be the best container? QVector <QObject> ?
    QVector sounds good, but as the element type you can use any data specific class you want.
    Again your code looks like you already have the data stored somewhere.

    Quote Originally Posted by sam_s View Post
    - If using QAbstractmodel is the best approach, then does every column need it's own get set function or can I populate each row with column / data as I was able to above?
    No. All data is accessed through the model's data() function.
    In QML each delegate just accesses its data by the role names for each data aspect/column.

    Quote Originally Posted by sam_s View Post
    Any direction or examples would help out greatly.
    I found a couple by googling for "c++ models for QML":
    http://doc.qt.io/qt-5/qtquick-modelv...model-subclass
    https://sailfishos.org/develop/tutor...ng-c-with-qml/

    Creating such a model is fairly easy
    1) Create a class derived from QAbstractListModel
    2) Add an enum for the data roles
    3) Implement roleNames() to provide a mapping between these enum values and the role names to use on QML side
    4) Implement rowCount() to return the number of rows you have data for
    5) Implement data() to return the value for a given row (from the index argument) and role (from the role argument)

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 5th April 2013, 04:58
  2. most efficient way to get a QPixmap from a QImage
    By caduel in forum Qt Programming
    Replies: 3
    Last Post: 11th June 2011, 12:00
  3. Replies: 1
    Last Post: 23rd September 2010, 20:16
  4. How to make efficient apps?
    By Tomasz in forum Qt for Embedded and Mobile
    Replies: 26
    Last Post: 22nd September 2010, 22:58
  5. Efficient way of inserting rows?
    By afflictedd2 in forum Qt Programming
    Replies: 1
    Last Post: 14th July 2008, 20:01

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.