+ Reply to Thread
Results 1 to 4 of 4

Thread: Multiple representation of same data (kgs and lbs)

  1. #1
    Join Date
    Jul 2017
    Posts
    5
    Qt products
    Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Multiple representation of same data (kgs and lbs)

    Good afternoon,

    I am working of a QML form where a user can enter a weight value in either lbs or kgs (using 2 different textedits). The value is maintained in a C++ structure (in kgs) and both representations are to be displayed. Is there a need for a model?

    Early tests also reveal a binding loop for property 'text' ... between the fields even when testing for a change in value before issuing an emit. In the QML file, I am using onTextChanged to update the other representation of the value.

    Any tip of the best approach?

    Regards,
    Daniel

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,550
    Thanks
    21
    Thanked 399 Times in 393 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple representation of same data (kgs and lbs)

    The value is maintained in a C++ structure (in kgs)
    From how you described it, it sounds you only have one data field, the kg value.
    Why use a struct for that?
    You can simply have in a member variable and expose it to QML via property.
    For one data filed a model would not be needed.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
    Watch these videos, and read his books they are must if you want to write clean code:
    https://sites.google.com/site/unclebobconsultingllc/

    Recommended off-line reading Qt:
    The Foundations of Qt Development (ISBN: 1-59059-831-8)

  3. #3
    Join Date
    Jul 2017
    Posts
    5
    Qt products
    Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Multiple representation of same data (kgs and lbs)

    Hi again,

    Thanks for the response. To answer the question Why use a struct for that?: The height and weight values are maintained in C++ class, stored and recalled from a DB.

    The operator can enter either imperial or metric values. The GUI presents TextInputs for both representations, each updated when values are entered; ie. enter 100kg, 220 lbs is also shown. Only the metric values are stored, Javascript functions are defined to do the conversion.

    I am using Q_PROPERTY in the C++ code, and slots are defined to handle the height and weight changes as shown here:
    Qt Code:
    1. Q_PROPERTY(int heightCm READ getHeightCm WRITE setHeightCm NOTIFY heightCmChanged)
    2. Q_PROPERTY(int weightKg READ getWeightKg WRITE setWeightKg NOTIFY weightKgChanged)
    3. ...
    4.  
    5. int getWeightKg() {
    6. return weightKg;
    7. }
    8.  
    9. void setWeightKg( int v)
    10. {
    11. if (getWeightKg() == v) {
    12. return;
    13. }
    14.  
    15. weightKg = v;
    16. emit weightKgChanged();
    17. }
    To copy to clipboard, switch view to plain text mode 
    In the QML code, properties are defined for the imperial values, and onTextChanged() signals are used to handle changes in the TextInput
    Qt Code:
    1. // Define the imperial representation of the metric values of the patient height and weight
    2. property int heightFt : 0
    3. property int heightInches : 0
    4. property int weightLbs: 0
    5. property alias heightCms: patientHeightCm.text // TextInput for height
    6. property alias weightKg: patientWeightKg.text // TextInput for weight
    7.  
    8. ...
    9. TextInput {
    10. id: patientWeightLbs
    11. ...
    12. onTextChanged: {
    13. var totalKgs = ScreenFuncs.poundsToKg( patientWeightLbs.text) + 0.5;
    14. console.log( "patientWeightLbs: onTextChanged(); totalKgs: ", totalKgs)
    15. patientForm.weightKg = Math.floor( totalKgs); // local alias
    16. }
    17. ...
    18. TextInput {
    19. id: patientWeightKg
    20.  
    21. onTextChanged: {
    22. var totalLbs = ScreenFuncs.kgToPounds( patientWeightKg.text) + 0.5;
    23. PatientHelper.setPatientWeight( patientWeightKg.text) // C++ slot; will call the setter for the Q_PROPERTY
    24. }
    25. ...
    To copy to clipboard, switch view to plain text mode 
    The QML code also has a button used to clear the content of the from. The conversion works, however, clearing the form is not reflected in the TextInput elements. To clear the form, a call is made to a Q_INVOKABLE method, and emits are issued for all data members of the class. Everything in the form is cleared with the exception of the height and weight. These are the two fields where onTextChanged is used instead of Binding

    I would like to avoid defining the imperial representation in the C++ code. Although the height and weight data members are cleared, it feels as if the QML does do receive/process the NOTIFY. This fails even if the Q_PROPERTY setters are called when clearing the form.
    Any suggestions on what is missing?

    Thanks,
    Daniel

  4. #4
    Join Date
    Jul 2017
    Posts
    5
    Qt products
    Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Multiple representation of same data (kgs and lbs)

    Hi again,

    after some trials and errors I have resolved this issue. I was able to correct the binding loop errors by removing the aliases and calling the C++ slot directly when entering weight in pounds or height in ft and inches. So the final implementation using the sample code already given looks like this:
    Qt Code:
    1. Item {
    2. id: patientForm
    3.  
    4. // Define the observers; imperial representation of the metric values of the patient height and weight
    5. property int heightFt : 0
    6. property int heightInches : 0
    7. property int weightLbs: 0
    8. ...
    9. TextInput {
    10.  
    11. id: patientWeightLbs
    12.  
    13. onTextChanged: {
    14. var lbsValue = Number( patientWeightLbs.text);
    15. var totalKgs = (lbsValue > 0) ? Math.floor( ScreenFuncs.poundsToKg( lbsValue) + 0.5) : 0;
    16. PatientHelper.setPatientWeight( totalKgs) // C++ slot; will call the setter for the Q_PROPERTY; do not use an alias
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 
    When clearing the form, the local properties are also cleared.

    Does anyone now why the notification was not handled properly by the pound representation when using an alias for the metric (kg) representation?

    Daniel

+ Reply to Thread

Similar Threads

  1. Data Linking and Representation
    By fatsack in forum Qt Programming
    Replies: 6
    Last Post: 29th November 2016, 06:53
  2. Gameboard representation in QT
    By Brando753 in forum Newbie
    Replies: 1
    Last Post: 4th November 2013, 13:37
  3. true representation in binary or hex
    By Tadas in forum Newbie
    Replies: 12
    Last Post: 10th October 2010, 19:02
  4. Graphical representation of an array
    By ithinkso in forum Qt Programming
    Replies: 2
    Last Post: 5th March 2010, 23:30
  5. Tree related representation
    By aegis in forum Qt Programming
    Replies: 1
    Last Post: 12th August 2007, 12:20

Tags for this Thread

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