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. Use Signal in QML from C++

Use Signal in QML from C++

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 342 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.
  • Nio74N Offline
    Nio74N Offline
    Nio74
    wrote on last edited by
    #1

    Good morning, I have to use signal emited at call in the function c++ in to qml:

    void Backend::smbConnectedToServer()
    {
        emit smbConnected();
    }
    

    I have declared my backend.cpp as a context:

     QQmlContext *context = engine.rootContext();
        context->setContextProperty("backend",&backend);
    

    while on the tutorial why I am following it is declared on this way:

    qmlRegisterType<Backend>("io.qt.Backend", 1, 0, "Backend");
    

    and thi is his qml:

    Window {
        visible: true
        width: 700
        minimumWidth: 500
        height: 450
        minimumHeight: 200
        title: "Server"
        color: "#CED0D4"
    
        Backend {
            id: backend
            onSmbConnected: {
                ti.append(addMsg("Somebody has connected"));
            }
            onSmbDisconnected: {
                ti.append(addMsg("Somebody has disconnected"));
            }
            onNewMessage: {
                ti.append(addMsg("New message: " + msg));
            }
    
    
    
    
        }
    
        ColumnLayout
        {
            anchors.fill: parent
            anchors.margins: 10
    
            RowLayout {
                anchors.horizontalCenter: parent.horizontalCenter
    
                BetterButton {
                    id: btn_start
                    anchors.left: parent.left
                    text: "Start server"
                    color: enabled ? this.down ? "#78C37F" : "#87DB8D" : "gray"
                    border.color: "#78C37F"
                    onClicked: {
                        ti.append(addMsg(backend.startClicked()));
                        this.enabled = false;
                    }
                }
                BetterButton {
                    enabled: !btn_start.enabled
                    anchors.right: parent.right
                    text: "Stop server"
                    color: enabled ? this.down ? "#DB7A74" : "#FF7E79" : "gray"
                    border.color: "#DB7A74"
                    onClicked: {
                        ti.append(addMsg(backend.stopClicked()));
                        btn_start.enabled = true;
                        ti.append(add)
                    }
                }
            }
    
            LayoutSection {
                Layout.fillHeight: true
    
                ScrollView {
                    id: scrollView
                    anchors.fill: parent
    
                    TextArea {
                        id: ti
                        readOnly: true
                        selectByMouse : true
                        font.pixelSize: 14
                        wrapMode: TextInput.WrapAnywhere
                    }
                }
            }
    
            BetterButton {
                anchors.horizontalCenter: parent.horizontalCenter
                text: "Test connection"
                color: this.down ? "#6FA3D2" : "#7DB7E9"
                border.color: "#6FA3D2"
                onClicked: {
                    ti.append(addMsg(backend.testClicked()));
                }
            }
        }
    
    
    Component.onCompleted: {
        ti.text = addMsg("Application started\n- - - - - -");
    }
    
    function addMsg(someText)
    {
        return "[" + currentTime() + "] " + someText;
    }
    
    function currentTime()
    {
        var now = new Date();
        var nowString = ("0" + now.getHours()).slice(-2) + ":"
                + ("0" + now.getMinutes()).slice(-2) + ":"
                + ("0" + now.getSeconds()).slice(-2);
        return nowString;
    }
    
    }
    
    

    I can not understand how to do the same thing.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You are creating a new Backend object in your QML code so it's not going to be the same as the one you put in the context.

      I think you are looking for the Connections type.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2

      • Login

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