gpt4 book ai didi

python - QFileDialog 预览

转载 作者:太空宇宙 更新时间:2023-11-04 02:37:15 49 4
gpt4 key购买 nike

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

方法一:扩展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

这是翻译后的类(class)。我正在使用 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

方法二:独立窗口预览

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

最佳答案

当你放置 box = QVBoxLayout(self) 时,你正在放置一个新的布局,你必须做的是使用 QFileDialog 自己的 Qt,为此我们启用DontUseNativeDialog 标志,除了从框语句中删除 self 之外:

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

关于python - QFileDialog 预览,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47599170/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com