Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-86782

Add an exec() method to Dialog

XMLWordPrintable

    • Icon: Suggestion Suggestion
    • Resolution: Unresolved
    • Icon: P3: Somewhat important P3: Somewhat important
    • None
    • 5.15.1
    • Quick: Controls 2
    • None
    • All

      Consider two QML components named A and B, and a custom MessageDialog component that consist in a Dialog with a required message property. There is not an obvious way to reuse MessageDialog while keeping all three classes unaware of each other.

      The two obvious options are:

      1. Instantiate a MessageDialog for every message you want to handle
        With this the handling of messages is fully encapsulated, but there is an overload for the QML engine. Also, the amount of code will be significantly increased, making it more difficult to maintain.
      2. Instantiate MessageDialog only once, and handle all possible situations in the MessageDialog itself, or in its single instance. The amount of code is not increased and the QML engine is not overloaded, but there will not be any encapsulation: the MessageDialog will not only be aware of the existence of A and B, but it will also manage operations and access/modify properties.

      There are other options, like creating the MessageDialog dynamically, but I cannot think in one that makes the implementation small, intuitive, and not make the code difficult to understand.

      The purpose is adding a exec() function to Dialog in Qt Quick Controls 2 module that behaves like the one provided by the QDialog widget: blocks the application until the user closes the dialog by declining, and return the result code.

      Some years ago, when I was using widgets...

      bool MyWidget::processData(const QByteArray &data)
      {
          // ...
          if (result.empty()) {
              messageDialog.setMessage("Found nothing, try the hard method?");
              if (messageDialog.exec() == QDialog::Accepted) {
                  // ...
              }
          }
          // ...
      }
      

      Now, imagine this in Qt Quick Controls 2:

      onClicked() {
          messageDialog.message = "Remove all elements?"
          if (messageDialog.exec() === Dialog.Accepted) {
              model.clear()
          }
      }
      

      See? It's so beautiful... simple... short... concise... elegant... intuitive...

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            ceperez1996 Carlos Pérez
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:

                There are no open Gerrit changes