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. Passing QBitArrya to QML

Passing QBitArrya to QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
11 Posts 3 Posters 895 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.
  • D Offline
    D Offline
    Damian7546
    wrote on last edited by
    #1

    Hi,
    In my class I have code from this property:
    Q_PROPERTY(QBitArray statusPrinter READ statusPrinter WRITE setStatusPrinter NOTIFY statusPrinterChanged)

    In QML code I would like read this bits, I try in this way :

     Image {
            id: test
            anchors {
                left: parent.left
                leftMargin: 30
                top: keyInsert.bottom
                topMargin: 20
            }
            width: 70
            height: 70
            source: {
                if (systemController.statusPrinter(0) === 0)
                    return "qrc:/UI/Assets/ok.png"
                if (systemController.statusPrinter(0) === 1)
                    return "qrc:/UI/Assets/warrning.png"
            }
        }
    

    but it doesn't work. What can be wrong ?

    1 Reply Last reply
    0
    • D Damian7546

      I do not know how use it.... so in *.h I use in this way:

      Q_PROPERTY(QBitArray statusPrinter READ statusPrinter WRITE setStatusPrinter NOTIFY statusPrinterChanged)
      
      public:
      Q_INVOKABLE bool isBitSet(int bit);
      
      

      in definition in *.cpp

      const QBitArray &SystemController::statusPrinter() const
      {
          return m_statusPrinter;
      }
      
      void SystemController::setStatusPrinter(const QBitArray &newStatusPrinter)
      {
          if (m_statusPrinter == newStatusPrinter)
              return;
          m_statusPrinter = newStatusPrinter;
          emit statusPrinterChanged();
      }
      
      bool SystemController::isBitSet(int bit)
      {
          return statusPrinter[bit];
      
      }
      
      void SystemController::statusFromPrinter(QBitArray status)
      {
          setStatusPrinter(status);
      }
      

      In this way I get error in return statusPrinter[bit]; line ::
      systemcontroller.cpp:46:12: Reference to non-static member function must be called; did you mean to call it with no arguments? (fix available) insert '()'

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #5

      @Damian7546 said in Passing QBitArrya to QML:

      return statusPrinter[bit];

      Where do you get a C++ array named statusPrinter from? [Maybe I'm wrong, I don't do any QML :) ]. The error message actually tells you what is wrong; this is what you have:

      const QBitArray &SystemController::statusPrinter() const
      
      1 Reply Last reply
      2
      • D Offline
        D Offline
        Damian7546
        wrote on last edited by Damian7546
        #2

        Probably I cannot pass an array directly to QML, so I need try in other way, like this:

        class SystemController : public QObject
        {
        Q_PROPERTY(QList<StatusPrinter> statusPrinter READ statusPrinter WRITE setStatusPrinter NOTIFY statusPrinterChanged)
         .
         .
         .
         struct StatusPrinter
            {
                bool tempalert;
                bool printoutlost;
                bool paperjam;
                bool paperlow;
                bool outofpaper;
                bool poweron;
            };
        };
        

        And in QML:

        var a = systemController.statusPrinter[0].tempalert
        

        But still doesn't work, I have error:
        TypeError: Cannot read property '0' of undefined

        1 Reply Last reply
        0
        • F Offline
          F Offline
          freedbrt
          wrote on last edited by freedbrt
          #3

          Just write help function which do it for you in the c++, for example

          Q_INVOKABLE bool isBitSet(int bit) { return statusPrinter[bit]; }
          
          1 Reply Last reply
          0
          • D Offline
            D Offline
            Damian7546
            wrote on last edited by Damian7546
            #4

            I do not know how use it.... so in *.h I use in this way:

            Q_PROPERTY(QBitArray statusPrinter READ statusPrinter WRITE setStatusPrinter NOTIFY statusPrinterChanged)
            
            public:
            Q_INVOKABLE bool isBitSet(int bit);
            
            

            in definition in *.cpp

            const QBitArray &SystemController::statusPrinter() const
            {
                return m_statusPrinter;
            }
            
            void SystemController::setStatusPrinter(const QBitArray &newStatusPrinter)
            {
                if (m_statusPrinter == newStatusPrinter)
                    return;
                m_statusPrinter = newStatusPrinter;
                emit statusPrinterChanged();
            }
            
            bool SystemController::isBitSet(int bit)
            {
                return statusPrinter[bit];
            
            }
            
            void SystemController::statusFromPrinter(QBitArray status)
            {
                setStatusPrinter(status);
            }
            

            In this way I get error in return statusPrinter[bit]; line ::
            systemcontroller.cpp:46:12: Reference to non-static member function must be called; did you mean to call it with no arguments? (fix available) insert '()'

            JonBJ 1 Reply Last reply
            0
            • D Damian7546

              I do not know how use it.... so in *.h I use in this way:

              Q_PROPERTY(QBitArray statusPrinter READ statusPrinter WRITE setStatusPrinter NOTIFY statusPrinterChanged)
              
              public:
              Q_INVOKABLE bool isBitSet(int bit);
              
              

              in definition in *.cpp

              const QBitArray &SystemController::statusPrinter() const
              {
                  return m_statusPrinter;
              }
              
              void SystemController::setStatusPrinter(const QBitArray &newStatusPrinter)
              {
                  if (m_statusPrinter == newStatusPrinter)
                      return;
                  m_statusPrinter = newStatusPrinter;
                  emit statusPrinterChanged();
              }
              
              bool SystemController::isBitSet(int bit)
              {
                  return statusPrinter[bit];
              
              }
              
              void SystemController::statusFromPrinter(QBitArray status)
              {
                  setStatusPrinter(status);
              }
              

              In this way I get error in return statusPrinter[bit]; line ::
              systemcontroller.cpp:46:12: Reference to non-static member function must be called; did you mean to call it with no arguments? (fix available) insert '()'

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #5

              @Damian7546 said in Passing QBitArrya to QML:

              return statusPrinter[bit];

              Where do you get a C++ array named statusPrinter from? [Maybe I'm wrong, I don't do any QML :) ]. The error message actually tells you what is wrong; this is what you have:

              const QBitArray &SystemController::statusPrinter() const
              
              1 Reply Last reply
              2
              • D Offline
                D Offline
                Damian7546
                wrote on last edited by Damian7546
                #6

                @JonB

                Can you explain me ? And how to pass this bits to QML?

                JonBJ 1 Reply Last reply
                0
                • D Damian7546

                  @JonB

                  Can you explain me ? And how to pass this bits to QML?

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #7

                  @Damian7546
                  You have a "getter" method/function QBitArray &SystemController::statusPrinter(). You wrote statusPrinter[bit]. But you have to call the function, so statusPrinter() is necessary. The error message suggested

                  (fix available) insert '()'

                  To do it in one statement you will want

                      return statusPrinter()[bit];
                      // or
                      return statusPrinter().at(bit);
                  
                  1 Reply Last reply
                  2
                  • D Offline
                    D Offline
                    Damian7546
                    wrote on last edited by Damian7546
                    #8

                    Thank you very much.
                    Now I know how Q_INVOKABLE working.

                    The Q_INVOKABLE macro allows calling the function directly from QML, but I need
                    auto update bits value in QML when they change

                    In the public slots statusFromPrinter(QBitArray status) I pass 3 bits :

                    const QBitArray &SystemController::statusPrinter() const
                    {
                        return m_statusPrinter;
                    }
                    void SystemController::setStatusPrinter(const QBitArray &newStatusPrinter)
                    {
                        if (m_statusPrinter == newStatusPrinter)
                            return;
                        m_statusPrinter = newStatusPrinter;
                        emit statusPrinterChanged();
                    }
                    
                    bool SystemController::isBitSet(int bit)
                    {
                        return statusPrinter()[bit];
                    }
                    void SystemController::statusFromPrinter(QBitArray status)
                    {
                        qDebug()<< "Bits: " << status ;
                        setStatusPrinter(status);
                        
                    }
                    

                    How update code in QML when bits on change

                    source: {
                                if (systemController.isBitSet(1) === true)
                                    return "qrc:/UI/Assets/ok.png"
                                if (systemController.isBitSet(1) === false)
                                    return "qrc:/UI/Assets/warrning.png"
                            }
                    

                    @JonB Can you help me ?

                    JonBJ 1 Reply Last reply
                    0
                    • D Damian7546

                      Thank you very much.
                      Now I know how Q_INVOKABLE working.

                      The Q_INVOKABLE macro allows calling the function directly from QML, but I need
                      auto update bits value in QML when they change

                      In the public slots statusFromPrinter(QBitArray status) I pass 3 bits :

                      const QBitArray &SystemController::statusPrinter() const
                      {
                          return m_statusPrinter;
                      }
                      void SystemController::setStatusPrinter(const QBitArray &newStatusPrinter)
                      {
                          if (m_statusPrinter == newStatusPrinter)
                              return;
                          m_statusPrinter = newStatusPrinter;
                          emit statusPrinterChanged();
                      }
                      
                      bool SystemController::isBitSet(int bit)
                      {
                          return statusPrinter()[bit];
                      }
                      void SystemController::statusFromPrinter(QBitArray status)
                      {
                          qDebug()<< "Bits: " << status ;
                          setStatusPrinter(status);
                          
                      }
                      

                      How update code in QML when bits on change

                      source: {
                                  if (systemController.isBitSet(1) === true)
                                      return "qrc:/UI/Assets/ok.png"
                                  if (systemController.isBitSet(1) === false)
                                      return "qrc:/UI/Assets/warrning.png"
                              }
                      

                      @JonB Can you help me ?

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #9

                      @Damian7546
                      No, because I said I know nothing about QML. "How update code in QML when bits on change": if you mean the bits change externally and you want something to update in QML when they do, I would guess you need to emit a signal when the bits get changed so that QML knows?

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        Damian7546
                        wrote on last edited by Damian7546
                        #10

                        @JonB
                        Calling setStatusPrinter(status) I emited signal statusPrinterChanged() , so in QML I should read statusPrinter but it doesn't work- I can't read array in QML in this way systemController.statusPrinter()........

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          Damian7546
                          wrote on last edited by Damian7546
                          #11

                          So, instead of using only one Q_PROPERTY(QBitArray status READ status WRITE setStatus NOTIFY statusChanged) a need use several Q_PROPERTY(bool statusX READ statusX WRITE setStatusX NOTIFY statusXChanged) to send my status to QML.
                          It not look optimally

                          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