Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved How to save code in QML

    QML and Qt Quick
    3
    5
    165
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A
      ACaldas last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • jeremy_k
        jeremy_k last edited by

        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?

        Asking a question about code? http://eel.is/iso-c++/testcase/

        A 1 Reply Last reply Reply Quote 1
        • 6thC
          6thC last edited by

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

          • https://doc.qt.io/qt-5/qsettings.html
          • https://doc.qt.io/qt-5/qml-qt-labs-settings-settings.html

          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.

          1 Reply Last reply Reply Quote 1
          • A
            ACaldas @jeremy_k last edited by

            @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.

            jeremy_k 1 Reply Last reply Reply Quote 0
            • jeremy_k
              jeremy_k @ACaldas last edited by jeremy_k

              @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.

              Asking a question about code? http://eel.is/iso-c++/testcase/

              1 Reply Last reply Reply Quote 2
              • First post
                Last post