2

I'm trying to add a default name to QFileDialog() the images below illustrate.

This is what I get (no filename)

What I get

and this is what I want to achieve without having to input it manually, I want to pass the file_name threw a function and have that name show up there. enter image description here

This is the code im trying to make to work:

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5 import *
import sys
class mainwindowUI(QMainWindow):
    def __init__(self):
        super(mainwindowUI, self).__init__()
        self.exportFiles('test.mp3')
    def exportFiles(self,file_name):
        filename, _ = QFileDialog.getSaveFileName(self, "Save audio file", "", "Audio Files (*.mp3)")
        if filename:
            print(filename)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = mainwindowUI()
    app.exec_()

I tried to add options:

filename, _ = QFileDialog.getSaveFileName(self, "Save audio file", "", "Audio Files (*.mp3)", options=QFileDialog.setLabelText(file_name))

But this is incorrect and i have no idea how to make it work...

Anyone know how to add a file name to save file dialog?

2 Answers 2

6

The third argument indicates the initial name:

def exportFiles(self, file_name):
    default_dir ="/home/qt_user/Documents"
    default_filename = os.path.join(default_dir, file_name)
    filename, _ = QFileDialog.getSaveFileName(
        self, "Save audio file", default_filename, "Audio Files (*.mp3)"
    )
    if filename:
        print(filename)
Sign up to request clarification or add additional context in comments.

3 Comments

That works perfectly, but What if I want to add a default directory location?
@JareBear So you have to concatenate the default directory with the file_name
This works perfectly! Exactly what i wanted, thank you so much.
1

First create a save-as action

self.saveas=QAction(QtGui.QIcon('saveas.png'),'save-as')

Add the save-as action to toolbar

toolbar=self.addToolbar('toolbar');
toolbar.addAction(self.saveas);

Sub this for your QFileDialog code

Fn, _=QFileDialog.getSaveFileName(self,'export pdf',file_name,'Pdf files(.pdf);;All files()');

when connecting the signal to the slot do this

Self.Saveas.toggled.connect(self.exportfiles('name of default file');

2 Comments

Your answer has some important issues. The toggled signal only works for checkable actions, so in this case the triggered signal should be used instead. That line also has wrong capitalization, the parenthesis is not closed (also, the semicolons are unnecessary) and the argument for the connection should be a callable, while in that case it will execute the call to exportfiles immediately and return None as soon as the dialog is closed, causing a crash.
Also, while QAction should accept a constructor without parent, in PyQt (at least up to 5.7) the parent argument is required in the constructor, so it's usually better to add it when creating standalone actions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.