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. Send text from Qt to QML
Forum Updated to NodeBB v4.3 + New Features

Send text from Qt to QML

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

    Hi,
    I have no idea what might be wrong :/
    I am beginneer in Qt and QML. firstly I exchange data beetwen QML and Qt by Q_PROPERTY macro.

    Now I create next class to exchange data, and finnaly I have three registered object to exchange data like below:
    main.cpp

        qmlRegisterType< PrinterSystemController >("MyPrinterController",1,0,"PrinterSystemController");
        qmlRegisterType< RfidSystemController    >("MyRfidController",1,0,"RfidSystemController");
        qmlRegisterType< NetworkSystemController >("MyNetworkController",1,0,"NetworkSystemController");
    
    

    mian.qml

     PrinterSystemController {
            id: printerSystemController
        }
    
        RfidSystemController {
            id: rfidSystemController
        }
    
        NetworkSystemController {
            id: networkSystemController
        }
    

    In PrinterSystemController and RfidSystemController everythings works. But in networSystemController works only Q_INVOKABLE metods calls form QML. When I try do some Q_PROPERTY :
    Q_PROPERTY(QString userName READ userName WRITE setUserName NOTIFY userNameChanged)

    Next I set some QStirng value by:

    setUserName(userNameInput);
    

    and read this property in qml to text:

       Text {
                text: networkSystemController.rfidnumber
                anchors.horizontalCenter: rectContuserTest.horizontalCenter
                anchors.verticalCenter: rectContuserTest.verticalCenter
                font.pixelSize: rectContuserTest.width * .15
                color: "white"
            }
    

    it doesn't work, without any warnings and erros.... what should I check ?

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

      @JoeCFD
      Yes, probably I found problem. QML create own object , and I created second object in Qt, so I worked from two different objects.

      I explain you .
      I have two class :
      NetworkSystemController
      NetworkProcessor
      The first class is to "data container" for QML visualisation, and them object is created by register this class in QML and import to QML.

      NetworkSystemController {
              id: networkSystemController
          }
      

      From my second class (NetworkProcessor) I would like to send userDetails variable , so I created object:

      NetworkSystemController * networkSystemController = new NetworkSystemController;
      connect(this, &NetworkProcessor::userDetails, networkSystemController,  &NetworkSystemController::userDetails);
      

      and slot in NetworkSystemController

      void NetworkSystemController::userDetails(QString userName)
      {
          setUserName(userName);
      }
      

      and setUserName metod :

      void NetworkSystemController::setUserName(const QString &newUserName)
      {
          if (m_userName == newUserName)
              return;
          m_userName = newUserName;
          emit userNameChanged();
           qDebug()<<"!!!here we are !!!";
      }
      
      

      is always called, but emit signal not emitted to object created by QML, am I right ? I would be really gratefull if someone give me a tips how to realize my concept without creating NetworkSystemController object outsied QML ?

      1 Reply Last reply
      0
      • JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by
        #2

        copy and paste error?

          Text {
                    text: networkSystemController.userName
                    anchors.horizontalCenter: rectContuserTest.horizontalCenter
                    anchors.verticalCenter: rectContuserTest.verticalCenter
                    font.pixelSize: rectContuserTest.width * .15
                    color: "white"
                }
        
        D JoeCFDJ 2 Replies Last reply
        0
        • D Offline
          D Offline
          Damian7546
          wrote on last edited by Damian7546
          #3

          No, it is not a problem, when I did like below , the text appear on screen:

          Text {
                      text: "Text test" ///networkSystemController.userName
                      anchors.horizontalCenter: rectContuserName.horizontalCenter
                      anchors.verticalCenter: rectContuserName.verticalCenter
                      font.pixelSize: rectContuserName.width * .15
                      color: "white"
                  }
          
          1 Reply Last reply
          0
          • JoeCFDJ JoeCFD

            copy and paste error?

              Text {
                        text: networkSystemController.userName
                        anchors.horizontalCenter: rectContuserTest.horizontalCenter
                        anchors.verticalCenter: rectContuserTest.verticalCenter
                        font.pixelSize: rectContuserTest.width * .15
                        color: "white"
                    }
            
            D Offline
            D Offline
            Damian7546
            wrote on last edited by Damian7546
            #4

            @JoeCFD I noiced that now my qtCreator in other way (propapbly after update) do Generate Missing Q_PROPERTY Members

            Currently result :
            first.jpg

            and before update it look like this:
            second.jpg

            I try rewrite my new Q_PROPERTY like old construction but still my NetworSystemController can't send QString to QML page. :/

            Any idea?

            JoeCFDJ 1 Reply Last reply
            0
            • D Damian7546

              @JoeCFD I noiced that now my qtCreator in other way (propapbly after update) do Generate Missing Q_PROPERTY Members

              Currently result :
              first.jpg

              and before update it look like this:
              second.jpg

              I try rewrite my new Q_PROPERTY like old construction but still my NetworSystemController can't send QString to QML page. :/

              Any idea?

              JoeCFDJ Offline
              JoeCFDJ Offline
              JoeCFD
              wrote on last edited by
              #5

              @Damian7546 "emit userNameChanged();" will update it.

              D 1 Reply Last reply
              0
              • JoeCFDJ JoeCFD

                @Damian7546 "emit userNameChanged();" will update it.

                D Offline
                D Offline
                Damian7546
                wrote on last edited by Damian7546
                #6

                @JoeCFD I do that from beginning.

                void NetworkSystemController::setUserName(const QString &newUserName)
                {
                    if (m_userName == newUserName)
                        return;
                    m_userName = newUserName;
                    emit userNameChanged();
                     qDebug()<<"!!!here we are !!!";
                }
                

                And in console I got message "!!!here we are !!!", and nothing in QML , so?

                JoeCFDJ 1 Reply Last reply
                0
                • D Damian7546

                  @JoeCFD I do that from beginning.

                  void NetworkSystemController::setUserName(const QString &newUserName)
                  {
                      if (m_userName == newUserName)
                          return;
                      m_userName = newUserName;
                      emit userNameChanged();
                       qDebug()<<"!!!here we are !!!";
                  }
                  

                  And in console I got message "!!!here we are !!!", and nothing in QML , so?

                  JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by
                  #7

                  @Damian7546 did you define the signal userNameChanged() in NetworkSystemController?

                  D 1 Reply Last reply
                  0
                  • JoeCFDJ JoeCFD

                    @Damian7546 did you define the signal userNameChanged() in NetworkSystemController?

                    D Offline
                    D Offline
                    Damian7546
                    wrote on last edited by
                    #8

                    @JoeCFD Exactly:

                    signals:
                        void userNameChanged();
                    
                    1 Reply Last reply
                    0
                    • JoeCFDJ JoeCFD

                      copy and paste error?

                        Text {
                                  text: networkSystemController.userName
                                  anchors.horizontalCenter: rectContuserTest.horizontalCenter
                                  anchors.verticalCenter: rectContuserTest.verticalCenter
                                  font.pixelSize: rectContuserTest.width * .15
                                  color: "white"
                              }
                      
                      JoeCFDJ Offline
                      JoeCFDJ Offline
                      JoeCFD
                      wrote on last edited by
                      #9

                      @JoeCFD add this in front of your Text to see what happens.

                      Connections { 
                              target: networkSystemController
                              function onUserNameChanged() {
                                    console.log( "===========")
                              }
                      }
                      
                      D 1 Reply Last reply
                      0
                      • JoeCFDJ JoeCFD

                        @JoeCFD add this in front of your Text to see what happens.

                        Connections { 
                                target: networkSystemController
                                function onUserNameChanged() {
                                      console.log( "===========")
                                }
                        }
                        
                        D Offline
                        D Offline
                        Damian7546
                        wrote on last edited by Damian7546
                        #10

                        @JoeCFD nothing....without logs after emit userNameChanged();

                              Connections {
                                    target: networkSystemController
                                    function onUserNameChanged() {
                                        console.log("===========")
                                    }
                                }
                                Text {
                        
                                    text: networkSystemController.userName
                                    anchors.horizontalCenter: rectContuserName.horizontalCenter
                                    anchors.verticalCenter: rectContuserName.verticalCenter
                                    font.pixelSize: rectContuserName.width * .15
                                    color: "white"
                                }
                        

                        What more can I check ?

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

                          I'm still struggling with mine problem, now I try called two functions like below:

                          void NetworkSystemController::companyDetails(QString userNamein)
                          {
                              setUserName(userNamein);
                          }
                          void NetworkSystemController::testFun()
                          {
                              setUserName("testowyName");
                          }
                          

                          and setUserName definition:

                          void NetworkSystemController::setUserName(const QString &newUserName)
                          {
                              if (m_userName == newUserName)
                                  return;
                              m_userName = newUserName;
                              emit userNameChanged();
                               qDebug()<<"!!!here we are !!!";
                          }
                          

                          In both calling I get message "!!!here we are !!!" in console, but in QML signal works only by testFun() but doesn't work when called companyDetails(QString userNamein)

                          Any idea?

                          1 Reply Last reply
                          0
                          • JoeCFDJ Offline
                            JoeCFDJ Offline
                            JoeCFD
                            wrote on last edited by
                            #12

                            @Damian7546 said in Send text from Qt to QML:

                            void NetworkSystemController::companyDetails(QString userNamein)
                            {
                            setUserName(userNamein);
                            }

                            change it here. You see the mismatch: QString userNamein and const QString &newUserName
                            void NetworkSystemController::companyDetails( const QString & userNamein)
                            {
                            setUserName(userNamein);
                            }

                            D 1 Reply Last reply
                            0
                            • JoeCFDJ JoeCFD

                              @Damian7546 said in Send text from Qt to QML:

                              void NetworkSystemController::companyDetails(QString userNamein)
                              {
                              setUserName(userNamein);
                              }

                              change it here. You see the mismatch: QString userNamein and const QString &newUserName
                              void NetworkSystemController::companyDetails( const QString & userNamein)
                              {
                              setUserName(userNamein);
                              }

                              D Offline
                              D Offline
                              Damian7546
                              wrote on last edited by Damian7546
                              #13

                              @JoeCFD said in Send text from Qt to QML:

                              @Damian7546 said in Send text from Qt to QML:

                              void NetworkSystemController::companyDetails(QString userNamein)
                              {
                              setUserName(userNamein);
                              }

                              change it here. You see the mismatch: QString userNamein and const QString &newUserName
                              void NetworkSystemController::companyDetails( const QString & userNamein)
                              {
                              setUserName(userNamein);
                              }

                              It isn't problem. I tested function like:

                              void NetworkSystemController::testFun(QString name)
                              {
                                  setUserName(QString);
                              }
                              

                              and works. but my still doesn't work :/

                              void NetworkSystemController::companyDetails(QString userNamein)
                              {
                                  setUserName(userNamein);
                              }
                              
                              

                              This two functions are only different call. First(good working) is called by button from visualisation, but companyDetails by emit signal from other object. ... And both functions are performed, but only first function emit signal works properly with qml ...

                              JoeCFDJ 1 Reply Last reply
                              0
                              • D Damian7546

                                @JoeCFD said in Send text from Qt to QML:

                                @Damian7546 said in Send text from Qt to QML:

                                void NetworkSystemController::companyDetails(QString userNamein)
                                {
                                setUserName(userNamein);
                                }

                                change it here. You see the mismatch: QString userNamein and const QString &newUserName
                                void NetworkSystemController::companyDetails( const QString & userNamein)
                                {
                                setUserName(userNamein);
                                }

                                It isn't problem. I tested function like:

                                void NetworkSystemController::testFun(QString name)
                                {
                                    setUserName(QString);
                                }
                                

                                and works. but my still doesn't work :/

                                void NetworkSystemController::companyDetails(QString userNamein)
                                {
                                    setUserName(userNamein);
                                }
                                
                                

                                This two functions are only different call. First(good working) is called by button from visualisation, but companyDetails by emit signal from other object. ... And both functions are performed, but only first function emit signal works properly with qml ...

                                JoeCFDJ Offline
                                JoeCFDJ Offline
                                JoeCFD
                                wrote on last edited by JoeCFD
                                #14

                                @Damian7546 qDebug()<<"!!!here we are !!!"; shows up in companyDetails() call?

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

                                  @JoeCFD
                                  Yes, probably I found problem. QML create own object , and I created second object in Qt, so I worked from two different objects.

                                  I explain you .
                                  I have two class :
                                  NetworkSystemController
                                  NetworkProcessor
                                  The first class is to "data container" for QML visualisation, and them object is created by register this class in QML and import to QML.

                                  NetworkSystemController {
                                          id: networkSystemController
                                      }
                                  

                                  From my second class (NetworkProcessor) I would like to send userDetails variable , so I created object:

                                  NetworkSystemController * networkSystemController = new NetworkSystemController;
                                  connect(this, &NetworkProcessor::userDetails, networkSystemController,  &NetworkSystemController::userDetails);
                                  

                                  and slot in NetworkSystemController

                                  void NetworkSystemController::userDetails(QString userName)
                                  {
                                      setUserName(userName);
                                  }
                                  

                                  and setUserName metod :

                                  void NetworkSystemController::setUserName(const QString &newUserName)
                                  {
                                      if (m_userName == newUserName)
                                          return;
                                      m_userName = newUserName;
                                      emit userNameChanged();
                                       qDebug()<<"!!!here we are !!!";
                                  }
                                  
                                  

                                  is always called, but emit signal not emitted to object created by QML, am I right ? I would be really gratefull if someone give me a tips how to realize my concept without creating NetworkSystemController object outsied QML ?

                                  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