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. Show different qml ui from c++
Forum Updated to NodeBB v4.3 + New Features

Show different qml ui from c++

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++qml
30 Posts 5 Posters 10.6k Views 5 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.
  • F Offline
    F Offline
    firefox
    wrote on 20 Apr 2016, 13:06 last edited by firefox
    #21

    folks,

    got another problem

    I have this signal messageReceivedToQml emitted by my c++ backend when a new message arrives.
    And I connected to my qml like this

    Connections {
            target: loginClass
            onMessageReceivedToQml: {
                console.log("in client connections")
                Script.createChatWindow()
            }
        }
    

    my createChatWindow javascript function is

    function createChatWindow() {
        console.log("creating chat window");
        var chatWindow2 = Qt.createComponent("ChatWindow.qml");
        chatWindow2.createObject(clientId);
    }
    
    

    the log 'creating chat window' gets printed when i receive a message but the chat window created is not visible. Am I missing something here?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 20 Apr 2016, 13:15 last edited by
      #22

      Hi
      I dont know QML but
      i was wondering if it needs something like
      chatWindow2.visible = true
      or
      chatWindow2.show()

      Sorry if its just silly. :)

      1 Reply Last reply
      0
      • F Offline
        F Offline
        firefox
        wrote on 20 Apr 2016, 13:39 last edited by firefox
        #23

        @mrjj Even I'm new to qml. nothing is sily to me :)

        I can't beleive i did the same mistake of using local variables instead of global. (Update: this is not the issue. Just had to delete the build folder and build again)
        I changed the code as below.

        var chatWindow;
        var sprite;
        function createChatWindow() {
            console.log("creating chat window");
            chatWindow = Qt.createComponent("ChatWindow.qml");
            sprite=chatWindow.createObject(clientId);
        }
        
        

        Update:Okay, It worked after the above changes. I just had to delete the build folder and build again.

        damn i edited the post instead of new post and everything is gone. edited again and added the code.

        1 Reply Last reply
        1
        • F Offline
          F Offline
          firefox
          wrote on 21 Apr 2016, 10:00 last edited by firefox
          #24

          Hi folks,

          Have an issue here. have a look at this code I'm trying.

          
          function handleMessage(from, msg) {
              var mysprite = loginClass.checkJid(from);  //This method returns the QQuickWindow object
              if(mysprite != "not found")
              {
                  console.log("in append block")   //This means this is a connected client and is not the first message. So we just append the message to chatAreaId(TextArea)
                  mysprite.chatAreaId.append(from+":"+msg);   //chatAreaId is the id of TextArea component 
              }
              else {
                  mysprite = createChatWindow();
                  console.log("in chat window block")    //This means the client is connecting to us for first time. so we need to create a dedicated chat window for the client  and add the id of client to backend with addToJids
                  loginClass.addToJids(from, mysprite);
                  mysprite.chatAreaId.append(from+":"+msg);
              }
          }
          
          function createChatWindow() {
              var chatWindow;
              var sprite;
              console.log("creating chat window");
              chatWindow = Qt.createComponent("ChatWindow.qml");
              sprite=chatWindow.createObject(clientId);   //This is a QQuickWindow
              return sprite;
          }
          

          So when I get a message I want to append it to TextArea component of my QQuickWindow object created dynamically with createObject.() function. But how do I access the qml components of QQuickWindow created dynamically?

          The above code gives an error saying "Cannot call method 'append' of undefined"

          1 Reply Last reply
          0
          • F Offline
            F Offline
            firefox
            wrote on 21 Apr 2016, 10:42 last edited by firefox
            #25

            Is this even a proper approach. I have a QHash in my c++ backend of type <QString, QQuickWindow>
            which stores key value pairs of clientId and chat window instance for the client.

            So when my application gets a incoming message, It first checks if there is already chat window instance for the client and gets the instance and appends the message to that window.

            If the client is connecting for first time, a new chat window is created and is added to QHash

            ? 1 Reply Last reply 21 Apr 2016, 10:54
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on 21 Apr 2016, 10:48 last edited by
              #26

              Please show us your checkJid() function.

              1 Reply Last reply
              0
              • F firefox
                21 Apr 2016, 10:42

                Is this even a proper approach. I have a QHash in my c++ backend of type <QString, QQuickWindow>
                which stores key value pairs of clientId and chat window instance for the client.

                So when my application gets a incoming message, It first checks if there is already chat window instance for the client and gets the instance and appends the message to that window.

                If the client is connecting for first time, a new chat window is created and is added to QHash

                ? Offline
                ? Offline
                A Former User
                wrote on 21 Apr 2016, 10:54 last edited by
                #27

                @firefox said:

                Is this even a proper approach.

                Well, you're building a backend in C++ and the GUI in QML. Now you start managing windows (parts of the GUI) in C++ again. Of course it will work but IMAO it's bad design. You should really do all GUI related stuff in QML.

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  firefox
                  wrote on 21 Apr 2016, 14:09 last edited by firefox
                  #28

                  Yeah. I don't want to deal with QQuickWindow objects in C++. The control already seems to sphagettified and confused to whoever is reading the code. I'm thinking of a different approach.

                  Here is the requirement.

                  When we get a message, C++ emits a signal messageReceived(from, message). It should be qml's responsibility to direct the message to appropriate chat window/create a new chat window if required.

                  The only thoughts I'm getting are creating map/hash like some data structure in qml that can hold a key and reference to chat window objects. I'm researching if it is possible in qml. Guess I'm still thinking in C++ way instead of qml way. You guys suggest anything ?

                  ? 1 Reply Last reply 21 Apr 2016, 16:27
                  0
                  • F firefox
                    21 Apr 2016, 14:09

                    Yeah. I don't want to deal with QQuickWindow objects in C++. The control already seems to sphagettified and confused to whoever is reading the code. I'm thinking of a different approach.

                    Here is the requirement.

                    When we get a message, C++ emits a signal messageReceived(from, message). It should be qml's responsibility to direct the message to appropriate chat window/create a new chat window if required.

                    The only thoughts I'm getting are creating map/hash like some data structure in qml that can hold a key and reference to chat window objects. I'm researching if it is possible in qml. Guess I'm still thinking in C++ way instead of qml way. You guys suggest anything ?

                    ? Offline
                    ? Offline
                    A Former User
                    wrote on 21 Apr 2016, 16:27 last edited by
                    #29

                    @firefox said:

                    When we get a message, C++ emits a signal messageReceived(from, message).

                    I wouldn't even do that. Better store all messages in a model in the backend, use a custom view to display / modify the data in QtQuick and let the model / view do all the synchronization.

                    F 1 Reply Last reply 22 Apr 2016, 18:04
                    0
                    • ? A Former User
                      21 Apr 2016, 16:27

                      @firefox said:

                      When we get a message, C++ emits a signal messageReceived(from, message).

                      I wouldn't even do that. Better store all messages in a model in the backend, use a custom view to display / modify the data in QtQuick and let the model / view do all the synchronization.

                      F Offline
                      F Offline
                      firefox
                      wrote on 22 Apr 2016, 18:04 last edited by firefox
                      #30

                      Hi guys,

                      An update.
                      So when I get the user gets a message, back-end parses the message and emits a messagaReceived(from,msg) signal.

                      Qml picks up the messageReceived signal and is connected to handleMessage javascript slot to direct the message to appropriate window.

                      var windows = [];
                      function handleMessage(from, msg) {
                      
                          for(var i=0; i<windows.length;i++) {
                              if(windows[i].jid == from) {
                                  console.log("FOUND MATCHING WINDOW");
                                  windows[i].chatBox.append(msg);
                                  return;
                              }
                          }
                          var mysprite = createChatWindow(from);
                          console.log("in chat window block")    //This means the client is connecting to us for first time. so we need to create a dedicated chat window for the client  and add the is of client to backend with addToJids
                          mysprite.chatBox.append(msg);
                      }
                      
                      function createChatWindow(from) {
                          var chatWindow;
                          var sprite;
                          console.log("creating chat window");
                          chatWindow = Qt.createComponent("ChatWindow.qml");
                          sprite=chatWindow.createObject(clientId,{"jid":from});  //This is a QQuickWindow
                          windows.push(sprite);
                          console.log("test "+sprite.jid);
                          return sprite;
                      }
                      
                      

                      I think I got the core functionality working. I'll be prettying up the UI little bit before adding other features.

                      @Wieland said:

                      @firefox said:

                      When we get a message, C++ emits a signal messageReceived(from, message).

                      I wouldn't even do that. Better store all messages in a model in the backend, use a custom view to display / modify the data in QtQuick and let the model / view do all the synchronization.

                      I tried this but couldn't get a working prototype because of my inexperience with models, views(guess qml in general). I'll try to refactor the above working method to something elegant soon. Thanks. :)

                      1 Reply Last reply
                      0

                      21/30

                      20 Apr 2016, 13:06

                      • Login

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