简体   繁体   中英

python pyqt5 add file name to getSaveFileName

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

This is what I get (no filename)

我得到的

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. 在此处输入图片说明

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?

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)

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM