Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QML c++ TCP Socket to instance connection in all classes



  • Good Night

    I have to initialize the opening of the server in the main.qml but that it is not work to all the classes, I tried to use on my main.qml:

    Component.onCompleted: {
    
    //method why connect to tcpSocket
            backend.connectClicked()
        }
    

    this is my instantiation c++ to qml

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include "backend.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
        
        //Pass my c++ to Qml
        qmlRegisterType<Backend>("io.qt.Backend", 1, 0, "Backend");
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    }
    
    

    this i my complete main.qml:

    import QtQuick 2.2
    import QtQuick.Controls 1.2
    import io.qt.Backend 1.0
    
    ApplicationWindow {
        width: 600
        height: 481
        visible: true
    
       Backend{
           id: backend
       }
    
        Rectangle {
            color: "#212126"
            anchors.fill: parent
        }
    
        toolBar: BorderImage {
            border.bottom: 8
            source: "images/toolbar.png"
            width: parent.width
            height: 70
    
            Rectangle {
                id: backButton
                width: opacity ? 60 : 0
                anchors.left: parent.left
                anchors.leftMargin: 20
                opacity: stackView.depth > 1 ? 1 : 0
                anchors.verticalCenter: parent.verticalCenter
                antialiasing: true
                height: 60
                radius: 4
                color: backmouse.pressed ? "#222" : "transparent"
                Behavior on opacity { NumberAnimation{} }
                Image {
                    anchors.verticalCenter: parent.verticalCenter
                    source: "images/navigation_previous_item.png"
                }
                MouseArea {
                    id: backmouse
                    anchors.fill: parent
                    anchors.margins: -10
                    onClicked: stackView.pop()
                }
            }
    
            Text {
                font.pixelSize: 42
                Behavior on x { NumberAnimation{ easing.type: Easing.OutCubic} }
                x: backButton.x + backButton.width + 20
                anchors.verticalCenter: parent.verticalCenter
                color: "white"
                text: "Client Paolo"
            }
        }
    
        ListModel {
            id: pageModel
            ListElement {
                title: "Riparazioni"
                page: "Riparazioni.qml"
            }
            ListElement {
                title: "Magazzino"
                page: "Magazzino.qml"
            }
            /*
            ListElement {
                title: "ProgressBar"
                page: "content/ProgressBarPage.qml"
            }
            ListElement {
                title: "Tabs"
                page: "content/TabBarPage.qml"
            }
            ListElement {
                title: "TextInput"
                page: "content/TextInputPage.qml"
            }
            ListElement {
                title: "List"
                page: "content/ListPage.qml"
            }  */
        }
    
        StackView {
            id: stackView
            anchors.fill: parent
            // Implements back key navigation
            focus: true
            Keys.onReleased: if (event.key === Qt.Key_Back && stackView.depth > 1) {
                                 stackView.pop();
                                 event.accepted = true;
                             }
    
            initialItem: Item {
                width: parent.width
                height: parent.height
                ListView {
                    model: pageModel
                    anchors.fill: parent
                    delegate: AndroidDelegate {
                        text: title
                        onClicked: stackView.push(Qt.resolvedUrl(page))
                    }
                }
            }
        }
    
        Component.onCompleted: {
            backend.connectClicked()
        }
    
    }
    
    

    this is other class where I use method for pass data at server

    import QtQuick 2.12
    import QtQuick.Controls 2.5
    import io.qt.Backend 1.0
    
    Item
    {
        id: element
    
        Backend {
            id: backend
        }
    
        Row {
            id: row1
            height: 50
            anchors.right: parent.right
            anchors.rightMargin: 5
            anchors.left: parent.left
            anchors.leftMargin: 5
            anchors.top: parent.top
            anchors.topMargin: 3
    
            Rectangle {
                id: rectangle_Search
                height: 44
                color: "#0e99b2"
                anchors.top: parent.verticalCenter
                anchors.topMargin: -25
                anchors.left: parent.left
    
                anchors.right: parent.right
    
    
                Text {
                    id:lbl_SearchCode
                    color: "#0a0808"
                    text: qsTr("Codice")
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.left: parent.left
                    anchors.leftMargin: 4
                    font.pixelSize: 12
                }
    
                CustomTextEdit {
                    id: c_Txt_codice
                    y: 0
                    width: 50
                    height: 20
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.bottom: parent
                    anchors.bottomMargin: 0
                    anchors.left: lbl_SearchCode.right
                    anchors.leftMargin: 20
    
                }
    
                CustomButton {
                    id: btnSearchKey
                    x: 583
                    y: 3
                    text: "S"
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.right: parent.right
                    anchors.rightMargin: 5
                    anchors.top: parent.verticalCenter
                    anchors.topMargin: -22
                    color: enabled ? this.down ? "#78C37F" : "#87DB8D" : "gray"
                    onClicked: {
                        backend.connectClicked()
                        backend.sendClicked(c_Txt_codice.text)
                    }
                }
    
            }
        }
        Row {
            id: row2
            height: 200
            anchors.right: parent.right
            anchors.rightMargin: 3
            anchors.left: parent.left
            anchors.leftMargin: 3
            anchors.top: row1.bottom
            anchors.topMargin: 2
    
            Rectangle{
                id: rectangle_Articolo
                color: "#0e99b2"
                anchors.fill: parent
    
    
    
                Label{
                    id: lbl_NBusta
                    color: "#ebedef"
                    text: "N°Busta"
                    anchors.left: parent.left
                    anchors.leftMargin: 5
                    anchors.top: parent.top
                    anchors.topMargin: 10
                }
    
                Text {
                    id: txt_NBusta
                    width: 40
                    height: 15
                    text: qsTr("Text")
                    anchors.top: parent.top
                    anchors.topMargin: 10
                    anchors.left: lbl_NBusta.right
                    anchors.leftMargin: 5
                    font.pixelSize: 12
                }
    
            }
    
        }
    }
    
    
    
    

    but if not use " backend.connectClicked()" not work

    error:

    QIODevice::write (QTcpSocket): device not open
    

  • Lifetime Qt Champion

    Hi,

    Based on the method name, why should the backend connect to the GUI ?

    Also, since part of the error message comes from that Backend class, you should share its implementation.



  • @SGaist said in QML c++ TCP Socket to istance connection in all classes:

    Hi,
    Based on the method name, why should the backend connect to the GUI ?
    Also, since part of the error message comes from that Backend class, you should share its implementation.

    the backend is done in c ++ where I'm developing the reception and the data transfer QTCPSoket.Però if I instantiate the connection to the server on each class works correctly, but I would like that when the app starts the client connects only once and then be used on all the qml classes always if it will be possible.


  • Lifetime Qt Champion

    That I understood.

    If you want only one instance of your backed to be used, you can also simply make it a context property of your engine root context.

    That will likely simplify things.



  • @SGaist said in QML c++ TCP Socket to instance connection in all classes:

    That I understood.

    If you want only one instance of your backed to be used, you can also simply make it a context property of your engine root context.

    That will likely simplify things.

    Tanks I did as you said:

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

Log in to reply