【问题标题】:QFileDialog getSaveFileName filter only specifc driveQFileDialog getSaveFileName 仅过滤特定驱动器
【发布时间】:2015-03-13 04:02:32
【问题描述】:

我正在使用 pyqt,我刚刚创建了一个 Qfiledialog 来保存我的程序生成的 PDF,如下所示:

QtGui.QFileDialog.getSaveFileName(self, "Save file", "", ".pdf")

但文件必须保存在“P:\”,在任何文件夹中,但必须是“P:”。 我该怎么做?

【问题讨论】:

  • 为什么是`P:`?如果没有这样的驱动怎么办?
  • 这是一个远程应用程序。如果显示 c:,他们将保存在服务器上,并且 P: 它是每个人都可以访问的映射网络文件夹。相信我,人们对本地 PC、网络和远程 PC 感到困惑。

标签: python qt pyqt qfiledialog


【解决方案1】:

您需要directoryEnteredfileSelected 信号而不是模态getSaveFileName。一些伪代码:

self.dialog = QtGui.QFileDialog()
self.dialog.directoryEntered.connect(self.checkDir)
self.dialog.fileSelected.connect(self.saveFile)
self.dialog.setAcceptMode(QFileDialog.AcceptSave)
self.dialog.setFileMode(QFileDialog.AnyFile)
self.dialog.setDirectory("P:")
self.dialog.show()
....
def checkDir(self, directory):
    if not (directory.startsWith("P:")):
        self.dialog.setDirectory("P:")

def saveFile(self, fileName):
    directory = QtCore.QFileInfo(fileName).canonicalPath()

【讨论】:

  • 谢谢!但我还有两个问题。我做错了什么,我应该在 saveFile 中做什么?如何获取文件名和文件夹? def gera_pdf(self): dia = QtGui.QFileDialog() dia.directoryEntered.connect(self.checkDir) dia.fileSelected.connect(self.saveFile) dia.setDirectory("P:") dia.show() def checkDir( self, directory): 如果不是 (directory.startsWith("P:")): QtGui.QFileDialog.setDirectory("P:") def saveFile(self): 1==1 PASTED HEOT TOO: pastebin.com/Rg03xJB5
  • 快到了! TypeError: QDir.absoluteFilePath(QString): 没有足够的参数。应该是什么论据?
  • 我的错。使用QFileInfo.canonicalPath()
  • 完美!非常感谢。救了我的命。
猜你喜欢
  • 2017-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-11
  • 1970-01-01
  • 2010-12-08
  • 1970-01-01
  • 2022-01-23
相关资源
最近更新 更多