QFileDialog预览

时间:2017-12-01 18:12:19

标签: python pyqt pyqt5 qfiledialog

我正在尝试让Qt5 QFileDialog在选择打开时显示图像的预览。

方法1:扩展QFileDialog
我使用this implementation of the dialog并使用Qt 5.6.1。

现在我正在使用Qt 5.9.2并且它不再起作用,给出以下错误:

Traceback (most recent call last):
  File "main.py", line 74, in mouseDoubleClickEvent
    self.openFileDialog()
  File "main.py", line 123, in openFileDialog
    openDialog = QFileDialogPreview(self, 'Open file', './', self.getDialogFilter())
  File "QFileDialogPreview.py", line 22, in __init__
    self.layout().addLayout(box, 1, 3, 1, 1)
TypeError: addLayout(self, QLayout, stretch: int = 0): too many arguments

这是翻译的课程。我使用的是Python 3.5.2:

from PyQt5.QtWidgets import QFileDialog, QLabel, QVBoxLayout
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt

class QFileDialogPreview(QFileDialog):
    def __init__(self, parent, caption, direcotry, filter):
        super().__init__(parent, caption, direcotry, filter)

        self.setObjectName("FileDialogPreview")
        box = QVBoxLayout(self)

        self.setFixedSize(self.width() + 250, self.height())

        self.mpPreview = QLabel("Preview", self)
        self.mpPreview.setFixedSize(250, 250)
        self.mpPreview.setAlignment(Qt.AlignCenter)
        self.mpPreview.setObjectName("labelPreview")
        box.addWidget(self.mpPreview)

        box.addStretch()

        self.layout().addLayout(box, 1, 3, 1, 1)

        self.currentChanged.connect(self.onChange)
        self.fileSelected.connect(self.onFileSelected)
        self.filesSelected.connect(self.onFilesSelected)

        self._fileSelected = None
        self._filesSelected = None

    def onChange(self, path):
        pixmap = QPixmap(path)

        if(pixmap.isNull()):
            self.mpPreview.setText("Preview")
        else:
            self.mpPreview.setPixmap(pixmap.scaled(self.mpPreview.width(), self.mpPreview.height(), Qt.KeepAspectRatio, Qt.SmoothTransformation))

    def onFileSelected(self, file):
        self._fileSelected = file

    def onFilesSelected(self, files):
        self._filesSelected = files

    def getFileSelected(self):
        return self._fileSelected

    def getFilesSelected(self):
        return self._filesSelected

方法2:预览的单独窗口

我还尝试为预览添加一个单独的窗口。当它在QFileDialog中被选中时显示图像时,我无法将其附加到Dialog本身的一侧。
我无法使用dialog.gemoetry()上的mapToGlobal方法将Dialog的位置映射到全局屏幕坐标。我得到的是一个QRect包含位置0,0,在这种情况下大小不匹配100x30,而对话框大约是800x600,大致是2000,500。

2 个答案:

答案 0 :(得分:2)

当你放置box = QVBoxLayout(self)时,你要放置一个新的布局,你必须做的是使用QFileDialog自己的Qt,为此我们启用DontUseNativeDialog标志,除了删除来自框中的自我声明:

class QFileDialogPreview(QFileDialog):
    def __init__(self, *args, **kwargs):
        QFileDialog.__init__(self, *args, **kwargs)
        self.setOption(QFileDialog.DontUseNativeDialog, True)

        box = QVBoxLayout()

        self.setFixedSize(self.width() + 250, self.height())

        self.mpPreview = QLabel("Preview", self)
        self.mpPreview.setFixedSize(250, 250)
        self.mpPreview.setAlignment(Qt.AlignCenter)
        self.mpPreview.setObjectName("labelPreview")
        box.addWidget(self.mpPreview)

        box.addStretch()

        self.layout().addLayout(box, 1, 3, 1, 1)

        self.currentChanged.connect(self.onChange)
        self.fileSelected.connect(self.onFileSelected)
        self.filesSelected.connect(self.onFilesSelected)

        self._fileSelected = None
        self._filesSelected = None

    def onChange(self, path):
        pixmap = QPixmap(path)

        if(pixmap.isNull()):
            self.mpPreview.setText("Preview")
        else:
            self.mpPreview.setPixmap(pixmap.scaled(self.mpPreview.width(), self.mpPreview.height(), Qt.KeepAspectRatio, Qt.SmoothTransformation))

    def onFileSelected(self, file):
        self._fileSelected = file

    def onFilesSelected(self, files):
        self._filesSelected = files

    def getFileSelected(self):
        return self._fileSelected

    def getFilesSelected(self):
        return self._filesSelected

输出:

enter image description here

答案 1 :(得分:0)

除了有关该主题的@eyllanesc答案外,我最初无法弄清楚如何调用该类来专门打开多个文件(当然,预览仅在选择单个文件时起作用)。因此,以下几行显示了如何执行此操作。 (将其作为单独的答案而不是评论发布,因为代码包含多行)

## For selecting a single file
def openBtn_single_clicked(self):
    
    filedialog = QFileDialogPreview(self,"Open File",
        "","Image Files (*.png *.jpg *.jpeg)")
    filedialog.setFileMode(QFileDialog.ExistingFile)
    if filedialog.exec_() == QFileDialogPreview.Accepted:
        print(filedialog.getFileSelected())

    return


## For selecting multiple files
def openBtn_multiple_clicked(self):
    filedialog = QFileDialogPreview(self,"Open File",
        "","PDF Files (*.pdf)")
    filedialog.setFileMode(QFileDialog.ExistingFiles)
    if filedialog.exec_() == QFileDialogPreview.Accepted:
        print(filedialog.getFilesSelected())

    return

这两个功能都需要通过命令偏离方向连接到按钮上

openBtnSingleFile.clicked.connect(self.openBtn_single_clicked)
openBtnMultipleFiles.clicked.connect(self.openBtn_multiple_clicked)