Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QList<float> to QML : undefined value
Forum Updated to NodeBB v4.3 + New Features

QList<float> to QML : undefined value

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 1.5k Views 2 Watching
  • 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.
  • DoohamD Offline
    DoohamD Offline
    Dooham
    wrote on last edited by
    #1

    Hello,
    I am trying to pass a group of values from C++ to QML. In order to archive this, I have append the values that I want in a QList<float> and I made a Q_Property. I used QQmlRegisterType. I follow this process with QStrings and with another QList of doubles.
    These are the definitions of the funtions:

     Q_PROPERTY(QList<float> vfrHud READ vfrHud  NOTIFY vfrHudChanged)
     
    public:
    QList<float> vfrHud();
    
    signals:
     void vfrHudChanged();
    
    slots:
    void setVfrHud();
    
    private:
     QList<float> _vfrHud;
    
    

    And these are the funtions:

    void MyTcpSocket::setVfrHud()
    {
        _vfrHud=mensajeRec.getVfrHud2();
        qDebug()<<"TcpSocket Set";
        qDebug()<<_vfrHud;
    
        emit vfrHudChanged();
    
    }
    
    QList<float> MyTcpSocket::vfrHud()
    {
        qDebug()<<"Hola";
        qDebug()<<_vfrHud[1];
        return _vfrHud;
    }
    

    As you see, I create a QList from a function of that is defined in another part of the code and the function setVfrHud is called by a signal-slots method. I haven 't any trouble with this part. My problem is that, when I emit the signal vfrHudChanged, the function vfrHud doesn't appears, so when I use the value in my QML code, this value is undefined. Anyone knows how to solve this?

    raven-worxR 1 Reply Last reply
    0
    • DoohamD Dooham

      Hello,
      I am trying to pass a group of values from C++ to QML. In order to archive this, I have append the values that I want in a QList<float> and I made a Q_Property. I used QQmlRegisterType. I follow this process with QStrings and with another QList of doubles.
      These are the definitions of the funtions:

       Q_PROPERTY(QList<float> vfrHud READ vfrHud  NOTIFY vfrHudChanged)
       
      public:
      QList<float> vfrHud();
      
      signals:
       void vfrHudChanged();
      
      slots:
      void setVfrHud();
      
      private:
       QList<float> _vfrHud;
      
      

      And these are the funtions:

      void MyTcpSocket::setVfrHud()
      {
          _vfrHud=mensajeRec.getVfrHud2();
          qDebug()<<"TcpSocket Set";
          qDebug()<<_vfrHud;
      
          emit vfrHudChanged();
      
      }
      
      QList<float> MyTcpSocket::vfrHud()
      {
          qDebug()<<"Hola";
          qDebug()<<_vfrHud[1];
          return _vfrHud;
      }
      

      As you see, I create a QList from a function of that is defined in another part of the code and the function setVfrHud is called by a signal-slots method. I haven 't any trouble with this part. My problem is that, when I emit the signal vfrHudChanged, the function vfrHud doesn't appears, so when I use the value in my QML code, this value is undefined. Anyone knows how to solve this?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Dooham
      see https://doc.qt.io/qt-5/qtqml-cppintegration-data.html#sequence-type-to-javascript-array
      only QList<double> is implicitly converted to a JS array.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      DoohamD 1 Reply Last reply
      3
      • raven-worxR raven-worx

        @Dooham
        see https://doc.qt.io/qt-5/qtqml-cppintegration-data.html#sequence-type-to-javascript-array
        only QList<double> is implicitly converted to a JS array.

        DoohamD Offline
        DoohamD Offline
        Dooham
        wrote on last edited by
        #3

        @raven-worx Thanks for your help but I have changed the QList<float> for QList<double> and I am still having the same trouble. The function read of the Q_Property isn't called when the signal vfrHudChanged() is emitted so the value of the property is not updated.

        raven-worxR 1 Reply Last reply
        0
        • DoohamD Dooham

          @raven-worx Thanks for your help but I have changed the QList<float> for QList<double> and I am still having the same trouble. The function read of the Q_Property isn't called when the signal vfrHudChanged() is emitted so the value of the property is not updated.

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @Dooham said in QList<float> to QML : undefined value:

          The function read of the Q_Property isn't called when the signal vfrHudChanged() is emitted so the value of the property is not updated.

          please post the updated C++ code and the QML code where you expect the read method to be called.
          The notifier signal only causes the read method to be called when there is a property binding existent (in your QML code).

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          DoohamD Pablo J. RoginaP 2 Replies Last reply
          1
          • raven-worxR raven-worx

            @Dooham said in QList<float> to QML : undefined value:

            The function read of the Q_Property isn't called when the signal vfrHudChanged() is emitted so the value of the property is not updated.

            please post the updated C++ code and the QML code where you expect the read method to be called.
            The notifier signal only causes the read method to be called when there is a property binding existent (in your QML code).

            DoohamD Offline
            DoohamD Offline
            Dooham
            wrote on last edited by
            #5

            @raven-worx This is the updated code (the part of this property):

            
                Q_OBJECT
            
                Q_PROPERTY(QList<double> vfrHud READ vfrHud  NOTIFY vfrHudChanged)
            
            public:
              
                 QList<double> vfrHud();
            signals:
             
                  void vfrHudChanged();
            
            public slots:
            
                  void setVfrHud();
            
            private: 
                QList<double> _vfrHud;
            
            
            
            QList<double> MyTcpSocket::vfrHud()
            {
                qDebug()<<"Hola";
                qDebug()<<_vfrHud[1];
                return _vfrHud;
            }
            
            
            
            void MyTcpSocket::setVfrHud()
            {
                _vfrHud=mensajeRec.getVfrHud2();
                qDebug()<<"TcpSocket Set";
                qDebug()<<_vfrHud;
            
                emit vfrHudChanged();
            
            }
            
            

            I think that the error is in the signal vfrHudChanged(). Because I call the function setVfrHud() the fuction that will write the property thought a signal that is emitted every time I detect the properties has changed (I know that this part work, because I have used the qDebug function to show the value of the variable and that value is correct). But when this signal is emitted the function vfrHud is not called (I know that because the debugger doesnt show me the message "hola").
            I have used this method with a previous QList (that one was different) and this method worked but not it doesnt work.

            1 Reply Last reply
            0
            • raven-worxR raven-worx

              @Dooham said in QList<float> to QML : undefined value:

              The function read of the Q_Property isn't called when the signal vfrHudChanged() is emitted so the value of the property is not updated.

              please post the updated C++ code and the QML code where you expect the read method to be called.
              The notifier signal only causes the read method to be called when there is a property binding existent (in your QML code).

              Pablo J. RoginaP Offline
              Pablo J. RoginaP Offline
              Pablo J. Rogina
              wrote on last edited by
              #6

              @Dooham

              and the QML code where you expect the read method to be called.

              as @raven-worx asked, it would be interesting to check the QML side you're writing...

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              DoohamD 1 Reply Last reply
              1
              • Pablo J. RoginaP Pablo J. Rogina

                @Dooham

                and the QML code where you expect the read method to be called.

                as @raven-worx asked, it would be interesting to check the QML side you're writing...

                DoohamD Offline
                DoohamD Offline
                Dooham
                wrote on last edited by
                #7

                @Pablo-J.-Rogina @raven-worx Sorry, this is the QML part. The code continue with a StackLayout where there is some texts that I want change for the values from the QList, but the part where I introduce the C++ class is here:

                import QtQuick 2.9
                import QtQuick.Controls 2.5
                import QtQuick.Layouts 1.12
                import mi.socket 1.0
                import "Funciones.js" as Funciones;
                
                
                
                ApplicationWindow {
                    visible: true
                    width:500
                    height: 500
                    minimumHeight: 400
                    minimumWidth: 400
                    id: ventana;
                    property variant misAngulos: [1,2,3]
                    property variant vfrQml: [1.0 ,2.0 ,3.0 ,4.0 ]
                    color: "Gray";
                    title: "GroundStation"
                
                    MiSocket{
                        id: mySocket
                        onBatteryChanged: {
                            textBateria.text=battery;
                        }
                        onVibrationsChanged: {
                            textoC.text=vibrations;
                        }
                        onAttitudChanged: {
                            textoY.text=attitud;
                        }
                        onGlobalPositionChanged: {
                            textGlobalPosition.text=globalPosition;
                        }
                        onWindChanged: {
                            textWind.text=wind;
                        }
                
                        onMissionAckChanged: {
                            textPreassure.text=missionAck;
                        }
                
                
                
                
                        onAngulosChanged:{
                
                            ventana.misAngulos=angulos
                
                            angulo1.rotation=ventana.misAngulos[0]
                            angulo2.rotation=-ventana.misAngulos[1]
                            angulo3.rotation=-ventana.misAngulos[2]
                        }
                
                        onVfrHudChanged: {
                        ventana.vfrQml=vfrHud
                
                            velocidadGround.text=ventana.vfrQml[2]
                            altura.text=ventana.vfrQml[0]
                            climb.text=ventana.vfrQml[3]
                        }
                
                
                
                
                    }
                
                
                raven-worxR 1 Reply Last reply
                0
                • DoohamD Dooham

                  @Pablo-J.-Rogina @raven-worx Sorry, this is the QML part. The code continue with a StackLayout where there is some texts that I want change for the values from the QList, but the part where I introduce the C++ class is here:

                  import QtQuick 2.9
                  import QtQuick.Controls 2.5
                  import QtQuick.Layouts 1.12
                  import mi.socket 1.0
                  import "Funciones.js" as Funciones;
                  
                  
                  
                  ApplicationWindow {
                      visible: true
                      width:500
                      height: 500
                      minimumHeight: 400
                      minimumWidth: 400
                      id: ventana;
                      property variant misAngulos: [1,2,3]
                      property variant vfrQml: [1.0 ,2.0 ,3.0 ,4.0 ]
                      color: "Gray";
                      title: "GroundStation"
                  
                      MiSocket{
                          id: mySocket
                          onBatteryChanged: {
                              textBateria.text=battery;
                          }
                          onVibrationsChanged: {
                              textoC.text=vibrations;
                          }
                          onAttitudChanged: {
                              textoY.text=attitud;
                          }
                          onGlobalPositionChanged: {
                              textGlobalPosition.text=globalPosition;
                          }
                          onWindChanged: {
                              textWind.text=wind;
                          }
                  
                          onMissionAckChanged: {
                              textPreassure.text=missionAck;
                          }
                  
                  
                  
                  
                          onAngulosChanged:{
                  
                              ventana.misAngulos=angulos
                  
                              angulo1.rotation=ventana.misAngulos[0]
                              angulo2.rotation=-ventana.misAngulos[1]
                              angulo3.rotation=-ventana.misAngulos[2]
                          }
                  
                          onVfrHudChanged: {
                          ventana.vfrQml=vfrHud
                  
                              velocidadGround.text=ventana.vfrQml[2]
                              altura.text=ventana.vfrQml[0]
                              climb.text=ventana.vfrQml[3]
                          }
                  
                  
                  
                  
                      }
                  
                  
                  raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by raven-worx
                  #8

                  @Dooham said in QList<float> to QML : undefined value:

                  ventana.vfrQml=vfrHud

                  can you try ventana.vfrQml=mySocket.vfrHud instead. Does it make a difference?

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  DoohamD 1 Reply Last reply
                  2
                  • raven-worxR raven-worx

                    @Dooham said in QList<float> to QML : undefined value:

                    ventana.vfrQml=vfrHud

                    can you try ventana.vfrQml=mySocket.vfrHud instead. Does it make a difference?

                    DoohamD Offline
                    DoohamD Offline
                    Dooham
                    wrote on last edited by
                    #9

                    @raven-worx Thanks you, that works. But I dont understand what is the difference between vfrHud and mySocket.vfrHud

                    raven-worxR 1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      MrShawn
                      wrote on last edited by MrShawn
                      #10

                      You registered an instance of your MyTcpSocket class and the vfrHud is a property of that class.

                      vfrHud does not exist without your socket class instance.

                      1 Reply Last reply
                      2
                      • DoohamD Dooham

                        @raven-worx Thanks you, that works. But I dont understand what is the difference between vfrHud and mySocket.vfrHud

                        raven-worxR Offline
                        raven-worxR Offline
                        raven-worx
                        Moderators
                        wrote on last edited by raven-worx
                        #11

                        @Dooham
                        i guess the engine tries to find a variable named vfrHud in the current scope, which dont exist.
                        Normally you provide a parameter with the notifier signal which is accessed in the slot scope.
                        But with this way you explicitly read the property named vfrHud

                        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                        If you have a question please use the forum so others can benefit from the solution in the future

                        1 Reply Last reply
                        1

                        • Login

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