QFile behavior when canceling the Open File Dialog
-
I have noticed a strange (incorrect?) behavior with QFile when canceling the Open File Dialog in Windows. Here is the scenario:
- Click on the QFile control
- Select a file “file.ext” in the Open File Dialog and press OK.
- The QFile now displays “file.ext”
- Open the Open File Dialog and press Cancel
- The QFile is now empty (the “file.ext” has disappeared)
My expectation is that Cancel closes the Open File Dialog without changing the content of the QFile. In other words, canceling the Open File Dialog would leave the content of the QFile to “file.ext”.
Is there a way to change the QFile behavior? If not would you consider either changing it or having a property based on which the behavior switches from discard to keep?
-
Hi @tdumitr,
Not sure this is possible out of the box. This behavior is the same with a classic
<input type="file">
What you can do, but it’s more a hack than a solution, is to listen to
@input
event, and if the picked file is undefined, restore the previous picked:<q-file v-model="file" label="Pick one file" filled style="max-width: 300px" @input="filepicked" clearable ></q-file>
... data () { return { file: null, oldPickedFile: null } }, methods: { filepicked(file) { if (file !== void 0) { this.oldPickedFile = file } else { this.file = this.oldPickedFile } } } ...
In action :
https://codepen.io/cp-tof06/pen/KKpbKeLI suggest to add
clearable
prop toq-file
, allowing the user to clear the field, sincecancel
won’t do it anymore