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. Creating a C++ identifier for a dynamically generated object

Creating a C++ identifier for a dynamically generated object

Scheduled Pinned Locked Moved Solved QML and Qt Quick
9 Posts 3 Posters 1.2k 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.
  • S Offline
    S Offline
    Snorlax
    wrote on last edited by
    #1
    This post is deleted!
    1 Reply Last reply
    0
    • L Offline
      L Offline
      lemons
      wrote on last edited by lemons
      #2

      You could do something like this:

      class BackEnd : public QObject
      {
          Q_OBJECT
          Q_PROPERTY(QString userName READ userName WRITE setUserName NOTIFY userNameChanged)
      
      public:
      
      //main.cpp
      #include <QQmlContext>
      
      //...
      
      QQmlApplicationEngine engine;
      
      BackEnd myBackend;
      engine.rootContext()->setContextProperty("BACKEND",  &myBackend);
      
      //main.qml - quick and dirty example
      import QtQuick 2.12
      import QtQuick.Window 2.12
      import QtQuick.Controls 2.12
      import QtQuick.Layouts 1.12
      
      Window {
          id: app
          visible: true
          width: 640
          height: 480
      
          ColumnLayout {
              RowLayout {
      
                  Column {
                      spacing: 10
                      Text {
                          font.bold: true
                          text: "Value from C++ backend:"
                      }
      
                      Text {
                          // automatically re-evaluates based on the Q_PROPERTY NOTIFY signal
                          text: BACKEND.userName
                      }
                  }
                  Rectangle {
                      border.width: 2
                      border.color: "green"
                      width: childrenRect.width
                      height: childrenRect.height
                      TextField {
                          id: unserNameInput
                          padding: 10
                          color: "black"
                          placeholderText: "Enter Username"
                      }
                  }
              }
              Button {
                  Layout.alignment: Qt.AlignHCenter
                  padding: 20
                  text: "SAVE"
                  onClicked: {
                      // uses the Q_PROPERTY WRITE method
                      BACKEND.userName = unserNameInput.text
                  }
              }
          }
      
          // in case you need any additional signal handling
          Connections {
              target: BACKEND
              function onUserNameChanged() {
                  console.debug("BackEnd signal:", "userNameChanged")
              }
          }
      }
      
      S 1 Reply Last reply
      3
      • fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on last edited by
        #3

        @lemons said in Creating a C++ identifier for a dynamically generated object:

        engine.rootContext()->setContextProperty("BACKEND", &myBackend);

        It is a little off putting that your "backend" is yelling at you. ;-)
        Good example, btw.

        C++ is a perfectly valid school of magic.

        L 1 Reply Last reply
        0
        • fcarneyF fcarney

          @lemons said in Creating a C++ identifier for a dynamically generated object:

          engine.rootContext()->setContextProperty("BACKEND", &myBackend);

          It is a little off putting that your "backend" is yelling at you. ;-)
          Good example, btw.

          L Offline
          L Offline
          lemons
          wrote on last edited by
          #4

          @fcarney yeah, I am somehow used to capitalize the context properties :D
          for me, this makes the origin more clear, so I can better differentiate in the auto-completion menu...

          1 Reply Last reply
          1
          • L lemons

            You could do something like this:

            class BackEnd : public QObject
            {
                Q_OBJECT
                Q_PROPERTY(QString userName READ userName WRITE setUserName NOTIFY userNameChanged)
            
            public:
            
            //main.cpp
            #include <QQmlContext>
            
            //...
            
            QQmlApplicationEngine engine;
            
            BackEnd myBackend;
            engine.rootContext()->setContextProperty("BACKEND",  &myBackend);
            
            //main.qml - quick and dirty example
            import QtQuick 2.12
            import QtQuick.Window 2.12
            import QtQuick.Controls 2.12
            import QtQuick.Layouts 1.12
            
            Window {
                id: app
                visible: true
                width: 640
                height: 480
            
                ColumnLayout {
                    RowLayout {
            
                        Column {
                            spacing: 10
                            Text {
                                font.bold: true
                                text: "Value from C++ backend:"
                            }
            
                            Text {
                                // automatically re-evaluates based on the Q_PROPERTY NOTIFY signal
                                text: BACKEND.userName
                            }
                        }
                        Rectangle {
                            border.width: 2
                            border.color: "green"
                            width: childrenRect.width
                            height: childrenRect.height
                            TextField {
                                id: unserNameInput
                                padding: 10
                                color: "black"
                                placeholderText: "Enter Username"
                            }
                        }
                    }
                    Button {
                        Layout.alignment: Qt.AlignHCenter
                        padding: 20
                        text: "SAVE"
                        onClicked: {
                            // uses the Q_PROPERTY WRITE method
                            BACKEND.userName = unserNameInput.text
                        }
                    }
                }
            
                // in case you need any additional signal handling
                Connections {
                    target: BACKEND
                    function onUserNameChanged() {
                        console.debug("BackEnd signal:", "userNameChanged")
                    }
                }
            }
            
            S Offline
            S Offline
            Snorlax
            wrote on last edited by
            #5
            This post is deleted!
            L 1 Reply Last reply
            0
            • S Snorlax

              This post is deleted!

              L Offline
              L Offline
              lemons
              wrote on last edited by
              #6

              @Snorlax you have to set the context property before loading the initial url in main.cpp

              int main(int argc, char *argv[])
              {
                  // ...
                  QApplication app(argc, argv);
                  // ...
                  QQmlApplicationEngine engine;
                  // ...
                  BackEnd myBackend;
                  engine.rootContext()->setContextProperty("BACKEND",  &myBackend);
                  // ...
                  // context property used in connection must be set before this line
                  engine.load(url);
                  // ...
                  return app.exec();
              }
              
              S 1 Reply Last reply
              0
              • L lemons

                @Snorlax you have to set the context property before loading the initial url in main.cpp

                int main(int argc, char *argv[])
                {
                    // ...
                    QApplication app(argc, argv);
                    // ...
                    QQmlApplicationEngine engine;
                    // ...
                    BackEnd myBackend;
                    engine.rootContext()->setContextProperty("BACKEND",  &myBackend);
                    // ...
                    // context property used in connection must be set before this line
                    engine.load(url);
                    // ...
                    return app.exec();
                }
                
                S Offline
                S Offline
                Snorlax
                wrote on last edited by
                #7
                This post is deleted!
                L 1 Reply Last reply
                0
                • S Snorlax

                  This post is deleted!

                  L Offline
                  L Offline
                  lemons
                  wrote on last edited by lemons
                  #8

                  @Snorlax the signal is only triggered when the signal from the c++ file is emitted.
                  Therefore, you have to enter something in the input field and click the save button.

                  As the example returns if you set the same name again, it won't emit a signal if you click the save button multiple times without changing the input field value:

                  void BackEnd::setUserName(const QString &userName)
                  {
                      // do not proceed if current name is equal to new name
                      if (userName == m_userName)
                          return;
                      
                      m_userName = userName;
                      emit userNameChanged();
                  }
                  
                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Snorlax
                    wrote on last edited by
                    #9
                    This post is deleted!
                    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