Results 1 to 4 of 4

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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!

  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
    - 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
  •  
Qt is a trademark of The Qt Company.