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

Open/show and close/hide popup by QML code



  • There is a Q_PROPERTY in my registered C++ class:

    // C++ Class
    Q_PROPERTY(bool inProgress READ inProgress WRITE setInProgress NOTIFY inProgressChanged)
    

    ... based on which I intend to show a QML popup:

    Popup {
        id: popup
        visible: cppClass.inProgress // Bind visibility to C++ Q_PROPERTY
    }
    

    But the pop doesn't show up. If change visible to true the popup is always shown of course.

    Tried so far

    Tried to use signal/slot connections to open/show the popup, but doesn't work:

    Popup {
        id: popup
        visible: false
    
        Connections {
            target: cppClass
            onInProgressChanged: {
                if (cppClass.inProgress) {
                    console.log("open ...")                   // This text is logged correctly
                    popup.visible = Qt.binding(function(){return true}) // popup is NOT shown
                    popup.open()                                        // popup is NOT opened
                } else {
                    console.log("close ...")
                    popup.visible = Qt.binding(function(){return false})
                    popup.close()
                }
            }
        }
    }
    

    What am I missing?


  • Moderators

    Popup {
        id: popup
        visible: cppClass.onProgress // Bind visibility to C++ Q_PROPERTY
    }
    

    You have a typo here (should be inProgress, not onProgress).

    The code seems correct, it should work. Some suggestions:

    • make sure you don't have that typo in actual code :-)
    • perhaps inProgress is changed when some very heavy, CPU-blocking operation is ongoing? Then QML engine might not have time to show the GUI changes. Try setting inProgress first, then starting the CPU-heavy operation after some time, or in a different thread
    • maybe you are setting inProgress to false immediately after you set it to true (the operation completes very fast)?
    • make sure the inProgressChanged signal is being emitted

  • Moderators

    Popup {
        id: popup
        visible: cppClass.onProgress // Bind visibility to C++ Q_PROPERTY
    }
    

    You have a typo here (should be inProgress, not onProgress).

    The code seems correct, it should work. Some suggestions:

    • make sure you don't have that typo in actual code :-)
    • perhaps inProgress is changed when some very heavy, CPU-blocking operation is ongoing? Then QML engine might not have time to show the GUI changes. Try setting inProgress first, then starting the CPU-heavy operation after some time, or in a different thread
    • maybe you are setting inProgress to false immediately after you set it to true (the operation completes very fast)?
    • make sure the inProgressChanged signal is being emitted


  • @sierdzio Thanks! There is no typo in the actual code. I'm going to check the other suggested items.



  • @sierdzio The code worked by triggering C++ signals further away from CPU-blocking operation.


Log in to reply