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

Function is erroniously being called multiple times.



  • I have a component which is establishing another component like so:

    Component
      {
        id: numericTextEntryComponent
    ...
    showNumericKeyboard("systemDialog", [qsTr("Cancel"), 
    qsTr("Finished")], 1, modelData, qsTr(""));
    ...
    

    the showNumericKeyboard function is instantiated in main like so:

    function showNumericKeyboard(objectName, modelButtons, primaryIndex, settingValue, hintMsg)
        {
             var comp = Qt.createComponent("qrc:/WizardPopupNumericKeyboard.qml");
             comp.createObject(application, {
                                  "objectName": objectName
                                  , "modelButtons": modelButtons
                                  , "primaryIndex": primaryIndex
                                  , "settingValue": settingValue
                                  , "hintMsg": hintMsg
                              })
        }
    

    I created a function called wizardBool in the back-end which simply swaps a bool and emits a signal:

    void SettingsPresenter::wizardBool()
    {
        m_wizardBool == false ? m_wizardBool = true : m_wizardBool = false;
        emit wizardBoolChanged();
    }
    

    I establish a connection to that signal like so:

    Connections
            {
              target: presentationManager.settingsPresenter
              onWizardBoolChanged:
              {
                console.info("Bool changed")
              }
            }
    

    I am calling the function (wizardBool()) whenever the WizardPopupNumericKeyboard component is destoryed:

    switch(index)
        {
        case 0:
          control.destroy()
          break;
        case 1:
          settingValue.value=Number(outValue)
          presentationManager.settingsPresenter.wizardBool() //here
          control.destroy();
          break;
         ...
    

    now for some reason, depending on where I establish that connection, the function is either being called multiple times or just once. For instance, if I establish the connection inside of WizardPopupNumericKeyboard.qml then it's only called once. However, if I establish the connection just prior to calling showNumericKeyboard:

    Connections
          {
            target: presentationManager.settingsPresenter
            onWizardBoolChanged:
            {
              console.info("Changed: ", )
            }
          }
    
          MouseArea
          {
            anchors.fill: parent
            onClicked:
            {
              showNumericKeyboard("systemDialog", [qsTr("Cancel"), 
              qsTr("Finished")], 1, modelData, qsTr(""));
         ...
    

    then, for some reason, it is called twice:
    output:

    qml: Bool Changed
    qml: Bool Changed
    

    Why is this happening, any ideas?

    EDIT: I put a print function in wizardbool() and apparently it's actually only being called once despite the print happening twice... still confused why that would happen.



  • Is there a chance you established the connection twice?



  • @Circuits hi
    i was trying to reply to this one when you marked it as SOLVED
    I think it's hard to guess what is going wrong in your project.
    in the other thread you said
    @Circuits said in Signals and slots C++ too QML:

    function that emit's a signal whenever it is called

    it looks suspect. In general you signal emit wizardBoolChanged() is in your setter method

     Q_PROPERTY(bool wizardBool READ wizardBool WRITE setWizardBool NOTIFY wizardBoolChanged)
    
    public:
        MyClass(QObject *parent = 0);
        ~MyClass();
    
        void setWizardBool(int wB)
        {
    //       if(m_wizardBool ==wB) return;
            m_wizardBool  = wB;
            emit wizardBoolChanged(m_wizardBool );
        }
        int wizardBool () const
        { return m_wizardBool ; }
    
    private :
    bool m_wizardBool ;
    

    you do

    m_wizardBool == false ? m_wizardBool = true : m_wizardBool = false;
    

    so you switch m_wizardBool s value calling the method From QML , then I don't get why you need any signal, if you call wizardBool() , then it will switch that value and that all ! Maybe im wrong, but it doesn't make sense

    in the example above you can directly get/set and catch signal of m_wizardBool from QML,

    //qml
    Component .onCompleted : {
      console.log(yourObject.wizardBool ) // get
      yourObject.wizardBool  = !yourObject.wizardBool  // set 
    
     
    }
    
    Connexions{ // signal catch
     target: yourObject
       wizardBoolChanged : {
     //
       }
    }
    

    Could you maybe explain in simple word what is your final aim ?



  • @LeLev I have a "Next" button which allows the user to progress through the wizard. However; I am trying to automate the process of progressing through the wizard as much as possible so that the user doesn't need to press "Next" each time. Instead, I am trying to create a way for the wizard to automatically progress so long as the value entered falls within an acceptable range. I had everything working (sort of) in that the user is not allowed to hit "finish" while the keypad is open if the value they entered did not fall within an acceptable range. So, at the time, it was okay to progress the wizard beforehand every time the keypad opened. However, if the user opens the keypad (progressing the wizard) and then hits "Cancel" then they land on the next page in the wizard, which doesn't make sense.

    So I had to rethink this idea and instead only progress the wizard in the event that the user hits "Finished". I figured an easy way to do that would be to create a bool on the back-end which can be toggled from the front end. That way, when they hit finish, the bool will toggle and I can progress the wizard like so:

    Connections
          {
            target: presentationManager.settingsPresenter
            onWizardBoolChanged:
            {
              //Here the bool will has been changed from false to true
              //and it can be changed back to false after the wizard progresses
              //haven't figured out how to deal with the infinite loop yet XD
            }
          }
    

    I think instead of just having it toggle the bool value I need to do that a different way like set the value to either true/false. Like you mentioned by having a setter function and a getter. I hope this explanation makes some sense, it's all rather rushed.



  • This post is deleted!

Log in to reply