Emitting multiple signals from the same onTriggered



  • Hi,

    I am having problems to emit 2 signals from the same custom QML Type. I want to run periodically check_conf(), and emit 1, 2 or none signals depending of check_ipConf() and check_gateway().

    The problem is that I am showing the ip and gateway values in two TextFields, and I have always updated the 2nd one (gateway value). The firs one related with the ip value never gets updated.

    I have the next code:

    main.qml

    Timer{
                interval: 1000; running: true; repeat:true
                onTriggered: cp4Conf.check_conf()
            }
    
            QTcp4Conf{
                id: cp4Conf
                onIpChanged: stateGroup.state = 'State1'
                onGatewayChanged : stateGroup.state = 'State2'
             }
    
                    State{
                        name: "State1"
    
                        PropertyChanges {
                            target: page.textIP
                            text: cp4Conf.ip
    
                        }
                    },
    
                    State{
                        name: "State2"
    
                        PropertyChanges {
                            target: page.textGateway
                            text: cp4Conf.gateway
    
                        }
                    }
    

    QTcp4Conf.cpp

    void QTcp4Conf::check_conf(){
    
        QString ipValue, gatewayValue;
    
    
        open_conf(ipValue,gatewayValue);
    
        check_ipConf(ipValue);
        check_gatewayConf(gatewayValue);
    }
    
    
    void QTcp4Conf::open_conf(QString& ipValue, QString& gatewayValue){
    
    
        QSettings fileConf("C:/Data/conf.conf",QSettings::IniFormat);
    
    
        ipValue = fileConf.value("static ip_address").toString();
        gatewayValue = fileConf.value("static routers").toString();
    
    
    }
    
    
    void QTcp4Conf::check_ipConf(QString ipValue){
    
        if(m_ip == ipValue){
            return;
        }
    
        m_ip = ipValue;
    
        emit ipChanged(m_ip);
    }
    
    void QTcp4Conf::check_gatewayConf(QString gatewayValue){
    
        if(m_gateway == gatewayValue){
            return;
        }
    
        m_gateway = gatewayValue;
    
        emit gatewayChanged(m_gateway);
    
    }
    
    QString QTcp4Conf::ip(){
      return m_ip;
    }
    
    QString QTcp4Conf::gateway(){
        return m_gateway;
    }
    
    

    Maybe I can't use this approach?

    Kind regards,

    Rafa


  • Lifetime Qt Champion

    Hi,

    Can you show your class definition ?
    Are you sure that signals are emitted ?


  • Moderators

    @rscr said in Emitting multiple signals from the same onTriggered:

    The problem is that I am showing the ip and gateway values in two TextFields

    Please post the code that shows how you connect your TextFields.



  • Hi,

    QTcp4Conf.h

    #ifndef QTCP4CONF_H
    #define QTCP4CONF_H
    
    
    #include <QObject>
    #include <QDebug>
    
    class QTcp4Conf : public QObject{
    
        Q_OBJECT
    
    
    
        Q_PROPERTY(QString ip
                   READ ip
                   NOTIFY ipChanged)
    
        Q_PROPERTY(QString gateway
                   READ gateway
                   NOTIFY gatewayChanged)
    
    public:
    
        explicit QTcp4Conf(QObject *parent = nullptr);
    
    
         Q_INVOKABLE void check_conf();
    
    
        QString ip();
        QString gateway();
    
        /**
         * @brief open_conf : Abrir archivo de configuracion (.conf)
         * @param ipValue   : Valor IP extraido del .conf
         * @param gatewayValue : Valor gateway extraido del .conf
         */
        void open_conf(QString& ipValue,QString& gatewayValue);
    
        /**
         * @brief check_ipConf : Comprobar si IP ha cambiado, en ese caso, lanzar aviso
         * @param ipValue      : Valor IP extraído del .conf
         */
        void check_ipConf(QString ipValue);
    
        /**
         * @brief check_gatewayConf : Comprobar si gateway ha cambiado, en ese caso, lanzar aviso
         * @param gatewayValue      : Valor gateway extraído del .conf
         */
        void check_gatewayConf(QString gatewayValue);
    
    signals:
    
        void ipChanged(QString ip);
        void gatewayChanged(QString gateway);
    
    private:
        QString m_ip;
        QString m_gateway;
    
    };
    
    
    #endif // QTCP4CONF_H
    

    QTcp4Conf.cpp

    #include "QTcp4Conf.h"
    #include <QSettings>
    
    
    
    QTcp4Conf::QTcp4Conf(QObject *parent):QObject(parent){
        m_ip = "";
        m_gateway = "";
    }
    
    
    void QTcp4Conf::check_conf(){
    
        QString ipValue, gatewayValue;
    
    
        open_conf(ipValue,gatewayValue);
    
        check_ipConf(ipValue);
        check_gatewayConf(gatewayValue);
    }
    
    
    void QTcp4Conf::open_conf(QString& ipValue, QString& gatewayValue){
    
    
        QSettings fileConf("C:/Data/conf.conf",QSettings::IniFormat);
    
    
        ipValue = fileConf.value("static ip_address").toString();
        gatewayValue = fileConf.value("static routers").toString();
    
    
    }
    
    
    void QTcp4Conf::check_ipConf(QString ipValue){
    
        if(m_ip == ipValue){
            return;
        }
    
        m_ip = ipValue;
    
        emit ipChanged(m_ip);
    }
    
    void QTcp4Conf::check_gatewayConf(QString gatewayValue){
    
        if(m_gateway == gatewayValue){
            return;
        }
    
        m_gateway = gatewayValue;
    
        emit gatewayChanged(m_gateway);
    
    }
    
    
    QString QTcp4Conf::ip(){
      return m_ip;
    }
    
    QString QTcp4Conf::gateway(){
        return m_gateway;
    }
    

    main.qml

    ApplicationWindow {
        visible: true
        width: 800
        height: 480
        title: qsTr("Tabs")
    
    
        SwipeView {
            id: swipeView
            width: 800
            anchors.fill: parent
    
    
            Timer{
                interval: 1000; running: true; repeat:true
                onTriggered: cp4Conf.check_conf()
            }
    
    
            // Configuracion de red de la RPI
            QTcp4Conf{
                id: cp4Conf
                onIpChanged: stateGroup.state = 'State1'
                onGatewayChanged : stateGroup.state = 'State2'
             }
    
            Page1Form {
                id:page
            }
    
            Page2Form {
                id:page2
            }
    
    
    
            StateGroup {
                id: stateGroup
                states: [
    
                    State{
                        name: "State1"
    
                        PropertyChanges {
                            target: page.textIP
                            text: cp4Conf.ip
    
                        }
                    },
    
                    State{
                        name: "State2"
    
                        PropertyChanges {
                            target: page.textGateway
                            text: cp4Conf.gateway
    
                        }
                    }
    
                ]
    
            }
    
        }
    
    
        PageIndicator{
            id: indicator
    
            count: swipeView.count
            currentIndex: swipeView.currentIndex
    
            anchors.bottom: swipeView.bottom
            anchors.horizontalCenter: parent.horizontalCenter
        }
    }
    

    That's all my code

    Kind Regards



  • Hi,
    
    Can you show your class definition ?
    Are you sure that signals are emitted ?
    

    If I have this in main.qml

    onIpChanged: stateGroup.state = 'State1'
    //onGatewayChanged : stateGroup.state = 'State2'

    The "textIP" TextField shows the desired value, so the ipChanged() is emited.

    If I have:

    onIpChanged: stateGroup.state = 'State1'
    onGatewayChanged : stateGroup.state = 'State2'

    Only the "textGateway" TextField is working

    Any idea?


  • Moderators

    @rscr said in Emitting multiple signals from the same onTriggered:

    If I have this in main.qml

    onIpChanged: stateGroup.state = 'State1'
    //onGatewayChanged : stateGroup.state = 'State2'

    The "textIP" TextField shows the desired value, so the ipChanged() is emited.

    If I have:

    onIpChanged: stateGroup.state = 'State1'
    onGatewayChanged : stateGroup.state = 'State2'

    Only the "textGateway" TextField is working

    Both of your signals are emitted.

    However, you cannot use a StateGroup to change your text like that. When it enters State1, then textGateway changes back to the default value. When it enters State2, then textIP changes back to the default value.

    Just update the text directly in the onIpChanged and onGatewayChanged signal handlers:

    QTcp4Conf {
        id: cp4Conf
        onIpChanged: page.textIP.text = cp4Conf.ip
        onGatewayChanged : page.textGateway.text = cp4Conf.gateway
    }
    


  • Thanks, that is working

    Kind Regards


Log in to reply