将变量传递到插槽QSignalMapp

2024-09-30 20:18:12 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description here

我正在尝试使用相同的图像上传功能为两个图像上传。你知道吗

所以点击上传按钮,我需要通过上传学生照片或者self.UploadParentsPhoto文件至self.UploadFile文件()功能,对应于按下哪个按钮。你知道吗

由于无法将变量传递到插槽中,因此我尝试使用QSignalMapper,如下所示。你知道吗

self.UploadStudentPhoto = QLineEdit() #Student Photo location
self.UploadParentsPhoto = QLineEdit() #Parents Photo location
self.ParentsImage = None

self.SignalMapper = QSignalMapper()
self.connect(self.ButtonUpload1,SIGNAL("clicked()"),self.SignalMapper, SLOT("map()"))
self.connect(self.ButtonUpload2,SIGNAL("clicked()"),self.SignalMapper,SLOT("map()"))

self.SignalMapper.setMapping(self.ButtonUpload1, self.UploadStudentPhoto)
self.SignalMapper.setMapping(self.ButtonUpload2, self.UploadParentsPhoto)

但我不确定下面这行需要在哪里向我为文件上传编写的函数传递信号(“mapped()”)。我应该如何写下这一行:

self.connect(self.SignalMapper,SIGNAL("mapped()"), self, self.UploadFile)

上传文件功能如下:

def UploadFile(self, ImagePath, ImageLabel):
    dir = os.path.dirname(".")
    formats = ["*.%s" % unicode(format).lower()\
        for format in QImageReader.supportedImageFormats()]
    self.fname = unicode(QFileDialog.getOpenFileName(self,"Image",dir,"Image (%s)" % " ".join(formats)))
    print self.fname
    ImagePath.setText(self.fname)
    ImagePath.selectAll()
    ImageLabel = QImage()
    ImageLabel.setPixmap(QPixmap.fromImage(ImageLabel))

我看到了许多QSignalMapper示例,但我不确定该变量到底是如何传递的。 请建议。你知道吗


Tags: 文件图像self功能signalconnect按钮fname
1条回答
网友
1楼 · 发布于 2024-09-30 20:18:12

实际上,它可以使用python中的partial模块将变量传递到connect&SLOT

在我看来,我建议使用“simple”(我认为)来实现,我将在下面的代码中向您展示

from functools import partial
.
.
.
        self.StudentPhoto = QLabel() # I know you have this.
        self.ParentsPhoto = QLabel() # I know you have this too.
        self.UploadStudentPhoto = QLineEdit()
        self.UploadParentsPhoto = QLineEdit()
        self.connect(self.ButtonUpload1, SIGNAL("clicked()"), partial(self.UploadFile, self.UploadStudentPhoto, self.StudentPhoto))
        self.connect(self.ButtonUpload2, SIGNAL("clicked()"), partial(self.UploadFile, self.UploadParentsPhoto, self.ParentsPhoto))
.
.
.
    def UploadFile(self, ImagePath, ImageLabel):
        dir = os.path.dirname(".")
        formats = ["*.%s" % unicode(format).lower()\
            for format in QImageReader.supportedImageFormats()]
        self.fname = unicode(QFileDialog.getOpenFileName(self,"Image",dir,"Image (%s)" % " ".join(formats)))
        print self.fname
        ImagePath.setText(self.fname)
        ImagePath.selectAll()
.
.
.

示例参考http://www.learnpython.org/en/Partial_functions

官方参考https://docs.python.org/2/library/functools.html


谨致问候

相关问题 更多 >