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

How to save code in QML



  • Hi all!

    I wonder if we could somehow save code in cases like this. Is it possible in a QML context?

    onToggled: {
                                        bc.checked = false
                                        bb.checked = false
                                        bd.checked = false
                                        spinBox1.enabled = false
                                        spinBox2.enabled = false
                                        spinBox3.enabled = false
                                        spinBox4.enabled = false
                                        spinBox5.enabled = false
                                        spinBox1.value = 0
                                        spinBox2.value = 0
                                        spinBox3.value = 0
                                        spinBox4.value = 0
                                        spinBox5.value = 0
                                    }
    

    Thanks for replying.



  • It's not clear to me what save means in this case. Is the goal to reduce the number of lines of code, write it to a file, write the values to a file, or something else?



  • I persist data for: application, runtime mode & user preference's via both:

    I typically persist single values and rather not complex types or anything - just strings and numbers. But I do also persist QColor types as well.

    I use labs qml settings for user preferences and on windows it dumps that in the registry, which is good as I like to keep separated multiple QSettings files for application config and different application mode config files.



  • @jeremy_k
    Exactly. There's so much lines for the same goal (to disable the instances spinBoxes).
    I thought that there should be a syntax in qml so that the action affects these five spinBox once instead of line by line.



  • @ACaldas If they're all visual children of a container item, this imperative block works:

    onToggled: {
        for (var child in parentItem.children) {
            if (undefined !== parentItem.children[child].checked) {
                parentItem.children[child].checked = false;
            }
            else if (undefined !== parentItem.children[child].enabled) {
                parentItem.children[child].enabled = false;
                parentItem.children[child].value = 0;
            }
        }
    }
    

    You might also look into either directly binding the values, or using a State. I don't think that these will result in fewer lines, but the relationships might be easier to understand and modify when necessary.


Log in to reply