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

Emit not fire when client connected to server.



  • So silly Question , but I have a server program . when I telnet to my app in command Ubuntu terminal I can see Hello message from my app(server) . I write emit in my code but it does not fire. WHY?!!
    this is my codes:
    main.cpp:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include "socketserver.h"
    
    int main(int argc, char *argv[])
    {
    
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        qmlRegisterType<SocketServer>("SocketServer",1,0,"SocketServer");
    
        const QUrl url(QStringLiteral("qrc:/main.qml"));
        QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                         &app, [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        }, Qt::QueuedConnection);
        engine.load(url);
    
    
        SocketServer svr;
        svr.connections();
    
        return app.exec();
    }
    
    

    SocketServer.h :

    #ifndef SOCKETSERVER_H
    #define SOCKETSERVER_H
    
    #include <QObject>
    #include <QDebug>
    #include <QTcpServer>
    #include <QTcpSocket>
    
    class SocketServer : public QObject
    {
        Q_OBJECT
        QString m_readst;
    
        bool m_cnn;
    
    public:
        explicit SocketServer(QObject *parent = nullptr);
    
        QTcpServer *server;
        QTcpSocket *socket;
        Q_PROPERTY(bool cnn READ cnn WRITE setCnn NOTIFY cnnChanged)
        Q_PROPERTY(QString readst READ readst WRITE setReadst NOTIFY readstChanged)
    
        QString readst() const;
    
        bool cnn() const;
    
    signals:
        void isConnected();
        void Connected();
    
        void readstChanged(QString readst);
    
        void cnnChanged(bool cnn);
    
    public slots:
        void connections();
        void connectionNew();
        void readServer();
        void setReadst(QString readst);
        void setCnn(bool cnn);
        void test();
    };
    
    #endif // SOCKETSERVER_H
    
    

    SocketServer.cpp :

    #include "socketserver.h"
    
    SocketServer::SocketServer(QObject *parent) : QObject(parent)
    {
    
    }
    
    QString SocketServer::readst() const
    {
        return m_readst;
    }
    
    bool SocketServer::cnn() const
    {
        return m_cnn;
    }
    
    void SocketServer::connections()
    {
        server=new QTcpServer(this);
        connect(server,&QTcpServer::newConnection,
                this,&SocketServer::connectionNew,Qt::UniqueConnection);
    //    connect(this,&SocketServer::Connected,
    //            this,&SocketServer::test);
        if(!server->listen(QHostAddress::Any,1234))
        {
    
        }
        else {
            qDebug()<<"connected";
            
    
        }
    }
    
    void SocketServer::connectionNew()
    {
        //setCnn(true);
        socket=server->nextPendingConnection();
        connect(socket,SIGNAL(readyRead()),
                this,SLOT(readServer()));
        socket->write("Hello\r\n");
        //socket->waitForBytesWritten(2000);
    
        emit isConnected();
    }
    
    void SocketServer::readServer()
    {
        setReadst(socket->readAll());
    }
    
    void SocketServer::setReadst(QString readst)
    {
        if (m_readst == readst)
            return;
    
        m_readst = readst;
        emit readstChanged(m_readst);
    }
    
    void SocketServer::setCnn(bool cnn)
    {
        if (m_cnn == cnn)
            return;
    
        m_cnn = cnn;
        emit cnnChanged(m_cnn);
    }
    
    void SocketServer::test()
    {
        emit isConnected();
    }
    
    

    and the last one is main.qml :

    import QtQuick 2.12
    import QtQuick.Controls 2.5
    import QtQuick.Window 2.12
    import SocketServer 1.0
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
        SocketServer{
            id:myserver
            onIsConnected:{
                lbl.text="Connected"
            }
        }
    //    Component.onCompleted:
    //    {
    //        myserver.connections();
    //    }
        Item {
            id: name
            Connections{
                target: myserver
                onCnnChanged:
                {
                    if(cnn)
                        lbl.text="Connected"
                    else
                        lbl.text="Disconnected"
                }
            }
        }
    
        Rectangle{
            anchors.fill: parent
            Column{
                width: parent.width
                height: parent.height
                Rectangle
                {
    //                color: "pink"
                    width: parent.width
                    height: 30
                    Label
                    {
                        id:lbl
                        text:"here"
                    }
    
                }
                Rectangle
                {
    //                color: "green"
                    border.width: 1
                    border.color: "gray"
                    width: parent.width
                    height: 200
                    Text{
                        id:txtrec
                        width: parent.width
                        height: parent.height
                    }
                }
                Rectangle
                {
    //                color: "red"
                    border.width: 1
                    border.color: "gray"
                    width: parent.width
                    height: 50
                    TextEdit{
                        id:txtsnd
                        width: parent.width
                        height: parent.height
                    }
                }
                Button
                {
                    width: parent.width
                    height: 50
                    text: "SEND"
                    onClicked: {myserver.test();}
    
                }
            }
        }
    }
    
    

    when I click the send button , emit is worked corect, but when I connet to my app ,emit not fire.![this image:](9d6a87ed-2957-4edf-b882-850cb452de00-image.png image url)


  • Lifetime Qt Champion

    @MhM93 said in Emit not fire when client connected to server.:

    I connet to my app ,emit not fire.!

    How do you know that it is not fired? Where do you show it? At least not in the code you posted.



  • @MhM93 said in Emit not fire when client connected to server.:

    SocketServer{
    id:myserver
    onIsConnected:{
    lbl.text="Connected"
    }
    }

    its in main.qml:

    SocketServer{
            id:myserver
            onIsConnected:{//<------------here 
                lbl.text="Connected"
            }
        }
    

  • Lifetime Qt Champion

    @MhM93 said in Emit not fire when client connected to server.:

    onIsConnected

    And where do you emit this signal?



  • @MhM93 said in Emit not fire when client connected to server.:

    void SocketServer::connectionNew()
    {
    //setCnn(true);
    socket=server->nextPendingConnection();
    connect(socket,SIGNAL(readyRead()),
    this,SLOT(readServer()));
    socket->write("Hello\r\n");
    //socket->waitForBytesWritten(2000);

    emit isConnected();
    

    }

    in SocketServer.cpp:

    void SocketServer::connectionNew()
    {
        //setCnn(true);
        socket=server->nextPendingConnection();
        connect(socket,SIGNAL(readyRead()),
                this,SLOT(readServer()));
        socket->write("Hello\r\n");
        //socket->waitForBytesWritten(2000);
    
        emit isConnected();
    }
    

  • Lifetime Qt Champion

    Sorry can't help any further - the qml stuff is simply crap - wonder how someone really can build something useful with this. Esp. with that few documentation. Even such a simple usecase as yours isn't properly documented... :(



  • you have created two SocketServer object, one in C side (in main.cpp) and one in Qml side (in main Qml)

    // create a new object
    SocketServer{
            id:myserver
            onIsConnected:{
                lbl.text="Connected"
            }
        }
    

    You need to share your C side server.
    in main.cpp

     SocketServer svr;
    engine.rootContext()->setContextProperty("cServer", &svr);
    

    and use it in Qml

    main.qml

        Connections {
            target: cServer
            onConnected: {
                lbl.text="Connected"
            }
            //  more one signal can be used
            onDisconnected: {
                lbl.text="disConnected"
            }
        }
    
    

    to avoid this error you can use 'qmlRegisterUncreatableType' instead 'qmlRegisterType'



  • @difix said in Emit not fire when client connected to server.:

    qmlRegisterUncreatableType

    really thanks. yes this is my problem. I create socket in main.cpp and also I create it again in qml


Log in to reply