Details
-
Suggestion
-
Resolution: Unresolved
-
P3: Somewhat important
-
None
-
5.15.1
-
None
Description
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:
- 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. - 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...
Attachments
Issue Links
- relates to
-
QTBUG-58620 Add async/await support to V4
- Reported