Clear the selected file of a file dialog

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Adab


I have a fileDialog node and I want to be able to clear (by that I mean have no file selected) the selected file using a button but I can’t find anything that seems to work.

I have tried fileDialog.deselect_items() method and changing the value of the current_path property but it seems to have no effect.

Here is what my UI looks like:

Nodes from left to right:

  • Label
  • LineEdit called “FilePath”
  • Button called “BrowseFileButton”
  • Button called “ClearFileButton”

And here is the code I am using:

extends HBoxContainer

onready var filePath = $FilePath
onready var browseFileBt = $BrowseFileButton
onready var fileDialog = $FileDialog

func _process(delta):
	filePath.text = fileDialog.current_file

func _on_BrowseFileButton_down():

func _on_ClearFileButton_down():

When clicking the clear button, the text remain.

fileDialog.deselect_items() seems to work here. Can you post some code?

jgodfrey | 2023-01-12 21:39

I have updated the post.

Adab | 2023-01-12 23:11

So, to make sure I understand… That’s custom UI you’ve created that interacts with the standard FileDialog, right? And, I assume the ... button is wired to _on_BrowseFileButton_down() and the x button is wired to _on_ClearFileButton_down()?

So, clicking ... seems to show the FileDialog as a popup. I’m confused about how you interact with the x button, as in “popup” mode, the file dialog normally auto-closes when you click outside of its borders (like, to click the “x”).

I guess I’d expect clicking “x” to close the file dialog, but also clear any selected file. Then, reopening the dialog with “…” I’d expect any previously selected file to be cleared.

Is that how this currently works or am I completely misinterpreting things?

Maybe this is a misunderstanding regarding what you expect to be cleared when you press “X”. Can you provide more details there?

jgodfrey | 2023-01-12 23:24

Sorry for the confusion, you got most of it right. So there is a text box called filepath, and as you can see in the _process() method, the text of filepath is always set to the selected file name. So if I open the file dialog using the ... button, select a file, and close the dialog, the name of the file will be displayed in the textbox.

When clicking the X button, it should clear the selected file, and therefore empty the text box since there are no selected file, but nothing seems to happen.

Adab | 2023-01-12 23:32

That’s helpful. And, are you using the FileDialog in single-file select mode or multi-file select mode?

jgodfrey | 2023-01-12 23:44

Also, you probably want to use the file_selected() signal to populate your LineEdit control instead of doing it continually in _process(). I think the functionality of the following is close to what you want?

func _on_BrowseFileButton_pressed():

func _on_ClearFileButton_pressed():
	$FileDialog.current_file = ""

func _on_FileDialog_file_selected(path):
	$FilePath.text = path

jgodfrey | 2023-01-13 00:02

Note, just applied a minor edit to the above code.

jgodfrey | 2023-01-13 00:03

I fail to see what actually fixed it because beside the filePath.clear() your code is pretty much the same as mine, but now it does work, thank you very much for your time :slight_smile:

Adab | 2023-01-13 10:11

Right. I think the main functional difference is that you weren’t explicitly clearing the contents of your LineEdit control. While it’s reasonable to think that shouldn’t be necessary, I couldn’t get this specific workflow to work as intended without doing that…

jgodfrey | 2023-01-13 14:46

:bust_in_silhouette: Reply From: jgodfrey

Based on the above discussions, the solution was:

func _on_BrowseFileButton_pressed():

func _on_ClearFileButton_pressed():
    $FileDialog.current_file = ""

func _on_FileDialog_file_selected(path):
    $FilePath.text = path