Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Unable to use "setContextObject" function twice
QtWS25 Last Chance

Unable to use "setContextObject" function twice

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 Posters 322 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    James A
    wrote on last edited by
    #1

    I have two classes defined in c++ , "ChargerInfo" & "BatteryInfo" which I am trying to expose to QML using contextObject

    ChargerInfo chargerInfo;
    BatteryInfo batteryInfo;
    
    context->setContextObject(&chargerInfo);
    context->setContextObject(&batteryInfo);
    

    when I call the setContextObject(&chargerInfo) function, it is working fine. But When I call the setContextObject(&batteryInfo) function second time for batteryinfo object
    I am not able to access the chargerInfo object in the QML . I am getting below error

    qrc:/main.qml:17: ReferenceError: output1 is not defined
    qrc:/main.qml:26: ReferenceError: output1 is not defined

    I am using version Qt 5.15.2 and added some part of source code below

    "ChargerInfo" class

    class ChargerInfo : public QObject{
    
        Q_OBJECT
    
        Q_PROPERTY(Output *output1 READ output1 WRITE setOutput1 NOTIFY output1Changed)
        Q_PROPERTY(Output *output2 READ output2 WRITE setOutput2 NOTIFY output2Changed)
        Q_PROPERTY(Output *output3 READ output3 WRITE setOutput3 NOTIFY output3Changed)
    

    BatteryInfo Class

    class BatteryInfo : public QObject
    {
        Q_OBJECT
    
        Q_PROPERTY(BatteryData *bat1 READ bat1 WRITE setBat1 NOTIFY bat1Changed)
        Q_PROPERTY(BatteryData *bat2 READ bat2 WRITE setBat2 NOTIFY bat2Changed)
        Q_PROPERTY(BatteryData *bat3 READ bat3 WRITE setBat3 NOTIFY bat3Changed)
    
    
    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      There can be only one context object. Use setContextProperty() instead, it will allow you to add as many objects as you like.

      (Z(:^

      1 Reply Last reply
      1
      • J Offline
        J Offline
        James A
        wrote on last edited by
        #3

        Thanks for the update. This is mentioned in Qt documentation , I was not able to find it

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          While not explicitly stated in the doc, you can see everything is in singular form.

          The getter and the setter talk about "the context object".

          If it was possible to set more than one, it would have been explicitly stated and you would also likely have had an overload taking a list or vector of objects.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • J Offline
            J Offline
            James A
            wrote on last edited by
            #5

            @SGaist

            Thanks for clarifying

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved