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. List of QPairs or QHash to qml

List of QPairs or QHash to qml

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 2 Posters 1.8k 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.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by
    #1

    Hi,
    I need a list Q_PROPERTY containing pair values like (150 , 300) that i can expose to qml and access it like

    myPropName[index].first and myPropName[index].second

    what is the best solution for this please ?

    I tryed :

    Q_PROPERTY(QQmlPropertyMap programSizes READ get_programSizes NOTIFY programSizesChanged)
    

    but im not sure this can be done, i have errors when trying to write accessor for it

    i also try to put QPairs in QVariantList but could not do it

    QPair<double, double> pairHeightWidth;
    pairHeightWidth.first= qAbs(progHeight)
    pairHeightWidth.second = qAbs(progWidth);
    /*QVariantList*/ m_progSizes << pairHeightWidth
    
    J.HilkJ 1 Reply Last reply
    0
    • J.HilkJ J.Hilk

      @LeLev im not sure if you can, as first and second are not defined as properties., inside the Pair class

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by ODБOï
      #10

      @J.Hilk

      did like this

      Q_PROPERTY(QVariantList progSizes READ getProgSizes WRITE setProgSizes NOTIFY progSizesChanged)
      private:
      QVariantList  m_progSizes 
      ... 
      ...{
              QVariantMap pair;
              pair.insert("width",999);
              pair.insert("height",66);
              m_progSizes << pair;
      }
      ...
      
       property variant ps: parser.progSizes
       onPsChanged: {
          console.log("prog 0 heigh/width : " + ps[0].height + "/" + ps[0].width)
      }
      

      @J-Hilk thanks for help

      1 Reply Last reply
      1
      • ODБOïO ODБOï

        Hi,
        I need a list Q_PROPERTY containing pair values like (150 , 300) that i can expose to qml and access it like

        myPropName[index].first and myPropName[index].second

        what is the best solution for this please ?

        I tryed :

        Q_PROPERTY(QQmlPropertyMap programSizes READ get_programSizes NOTIFY programSizesChanged)
        

        but im not sure this can be done, i have errors when trying to write accessor for it

        i also try to put QPairs in QVariantList but could not do it

        QPair<double, double> pairHeightWidth;
        pairHeightWidth.first= qAbs(progHeight)
        pairHeightWidth.second = qAbs(progWidth);
        /*QVariantList*/ m_progSizes << pairHeightWidth
        
        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #2

        @LeLev
        try it as a pure QVariant

        Q_PROPERTY(QVariant programSizes READ get_programSizes NOTIFY programSizesChanged)

        with
        QVariant get_programSizes(){QVariant::fromValue(m_programSizes);}


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        ODБOïO 1 Reply Last reply
        2
        • J.HilkJ J.Hilk

          @LeLev
          try it as a pure QVariant

          Q_PROPERTY(QVariant programSizes READ get_programSizes NOTIFY programSizesChanged)

          with
          QVariant get_programSizes(){QVariant::fromValue(m_programSizes);}

          ODБOïO Offline
          ODБOïO Offline
          ODБOï
          wrote on last edited by ODБOï
          #3

          @J.Hilk hi,
          thank you.
          I tryed like this :

           Q_PROPERTY(QVariant programSizes READ get_programSizes WRITE set_programSizes NOTIFY programSizesChanged)
          ...
          QPair<double, double> pairHeightWidth;
              pairHeightWidth.first = qAbs(progWidth);
              pairHeightWidth.second = qAbs(progHeight);
          
              m_progSizes << QVariant::fromValue(pairHeightWidth);
          
          

          this works but when i pass this Q_PROPERTY to another object through SIGNAL/SLOT

          QObject::connect(parser,&ProgramParser::programSizesChanged,this,&ThreadsafeParser::set_programSizes);
          

          i have this output :
          QObject::connect: Cannot queue arguments of type 'QVariant&'
          (Make sure 'QVariant&' is registered using qRegisterMetaType().)

          How/where i have to register QVariant& please ?

          J.HilkJ 1 Reply Last reply
          0
          • ODБOïO Offline
            ODБOïO Offline
            ODБOï
            wrote on last edited by
            #4

            making my signal arg const solved this error
            void programSizesChanged(const QVariant &mp);
            instead of
            void programSizesChanged( QVariant &mp);

            1 Reply Last reply
            0
            • ODБOïO ODБOï

              @J.Hilk hi,
              thank you.
              I tryed like this :

               Q_PROPERTY(QVariant programSizes READ get_programSizes WRITE set_programSizes NOTIFY programSizesChanged)
              ...
              QPair<double, double> pairHeightWidth;
                  pairHeightWidth.first = qAbs(progWidth);
                  pairHeightWidth.second = qAbs(progHeight);
              
                  m_progSizes << QVariant::fromValue(pairHeightWidth);
              
              

              this works but when i pass this Q_PROPERTY to another object through SIGNAL/SLOT

              QObject::connect(parser,&ProgramParser::programSizesChanged,this,&ThreadsafeParser::set_programSizes);
              

              i have this output :
              QObject::connect: Cannot queue arguments of type 'QVariant&'
              (Make sure 'QVariant&' is registered using qRegisterMetaType().)

              How/where i have to register QVariant& please ?

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #5

              @LeLev said in List of QPairs or QHash to qml:

              How/where i have to register QVariant& please ?

              you use qRegisterMetaType usually like this:

              qRegisterMetaType<MyStruct>();

              where you call it, doesn't matter much, just make sure that you call it before you make the first connection that uses the type


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              ODБOïO 1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                @LeLev said in List of QPairs or QHash to qml:

                How/where i have to register QVariant& please ?

                you use qRegisterMetaType usually like this:

                qRegisterMetaType<MyStruct>();

                where you call it, doesn't matter much, just make sure that you call it before you make the first connection that uses the type

                ODБOïO Offline
                ODБOïO Offline
                ODБOï
                wrote on last edited by ODБOï
                #6

                @J.Hilk thank you,
                sorry i made an error in my previously pasted code,

                this dont work, is m_programSizes is a QVariant

                QPair<double, double> pairHeightWidth;
                 pairHeightWidth.first = qAbs(progWidth);
                 pairHeightWidth.second = qAbs(progHeight);
                
                 m_programSizes << QVariant::fromValue(pairHeightWidth);
                
                1 Reply Last reply
                0
                • ODБOïO Offline
                  ODБOïO Offline
                  ODБOï
                  wrote on last edited by
                  #7

                  this works :

                  m_programSizes.setValue(QVariant::fromValue(pairHeightWidth));

                  but how to read this qml side now ?

                  i only got

                   property variant progSizes: parser.programSizes
                      onProgSizesChanged: {
                          for (var i in progSizes ){
                              console.log(i)
                          }
                  

                  output
                  QVariant(QPair<double,double>, )

                  ODБOïO J.HilkJ 2 Replies Last reply
                  0
                  • ODБOïO ODБOï

                    this works :

                    m_programSizes.setValue(QVariant::fromValue(pairHeightWidth));

                    but how to read this qml side now ?

                    i only got

                     property variant progSizes: parser.programSizes
                        onProgSizesChanged: {
                            for (var i in progSizes ){
                                console.log(i)
                            }
                    

                    output
                    QVariant(QPair<double,double>, )

                    ODБOïO Offline
                    ODБOïO Offline
                    ODБOï
                    wrote on last edited by
                    #8

                    @LeLev
                    i will try this
                    https://forum.qt.io/topic/87558/qlist-of-qpair-in-qml

                    1 Reply Last reply
                    0
                    • ODБOïO ODБOï

                      this works :

                      m_programSizes.setValue(QVariant::fromValue(pairHeightWidth));

                      but how to read this qml side now ?

                      i only got

                       property variant progSizes: parser.programSizes
                          onProgSizesChanged: {
                              for (var i in progSizes ){
                                  console.log(i)
                              }
                      

                      output
                      QVariant(QPair<double,double>, )

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #9

                      @LeLev im not sure if you can, as first and second are not defined as properties., inside the Pair class


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      ODБOïO 1 Reply Last reply
                      0
                      • J.HilkJ J.Hilk

                        @LeLev im not sure if you can, as first and second are not defined as properties., inside the Pair class

                        ODБOïO Offline
                        ODБOïO Offline
                        ODБOï
                        wrote on last edited by ODБOï
                        #10

                        @J.Hilk

                        did like this

                        Q_PROPERTY(QVariantList progSizes READ getProgSizes WRITE setProgSizes NOTIFY progSizesChanged)
                        private:
                        QVariantList  m_progSizes 
                        ... 
                        ...{
                                QVariantMap pair;
                                pair.insert("width",999);
                                pair.insert("height",66);
                                m_progSizes << pair;
                        }
                        ...
                        
                         property variant ps: parser.progSizes
                         onPsChanged: {
                            console.log("prog 0 heigh/width : " + ps[0].height + "/" + ps[0].width)
                        }
                        

                        @J-Hilk thanks for help

                        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