首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将变量传递到槽中- QSignalMapper

将变量传递到槽中- QSignalMapper
EN

Stack Overflow用户
提问于 2014-08-12 22:17:00
回答 1查看 103关注 0票数 0

我正在尝试使用相同的图片上传功能来上传这两张图片。

因此,在单击Upload按钮时,我需要将self.UploadStudentPhoto或self.UploadParentsPhoto传递给self.UploadFile()函数,对应于按下的按钮。

由于我们不能将变量传递到插槽中,所以我尝试使用QSignalMapper,如下所示。

代码语言:javascript
复制
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()”)传递给我为上传文件而编写的函数。下面这行我该怎么写:

代码语言:javascript
复制
self.connect(self.SignalMapper,SIGNAL("mapped()"), self, self.UploadFile)

上传文件功能如下:

代码语言:javascript
复制
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示例,但我不确定变量是如何传递的。请提个建议。

EN

回答 1

Stack Overflow用户

发布于 2014-08-12 22:41:12

实际上,通过使用python中的partial模块,它可以将一个变量传递给connect和SLOT;

就我的观点而言,我建议使用'simple‘(我认为)来实现,我将在下面的代码中向你展示;

代码语言:javascript
复制
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

致以敬意,

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25266674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档