PDA

View Full Version : Qt5 QFileDialog with preview picture



Daree1980
28th November 2017, 23:04
Hello,

I used Michal Fapsos implementation of this dialog (http://www.qtcentre.org/threads/33593-How-can-i-have-a-QFileDialog-with-a-preview-of-the-picture?p=247022#post247022) and it worked with Qt 5.6.1 .

Now I am using Qt 5.9.2 and it does not work anymore, giving the following error:


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

Here is the translated class. I am using 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.mpPrev iew.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


Has there been a change in the layout of QFileDialog, which should be a QGridLayout, but now is a QVBoxLayout, or is the problem somewhere else?

I am confident in the code working before, since it ran on my linux machine (Qt 5.6.1), but running it on my windows using (Qt 5.9.3) failed with the above error as well.