如何在具有值而不是单个变量值的列表上应用 PyQt5 事件? [英] How to apply PyQt5 Event on a list with values instead of single variable value?

查看:44
本文介绍了如何在具有值而不是单个变量值的列表上应用 PyQt5 事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于给定 here 的 SO 问题和答案,我修改了该值成为列表的答案.它给我一个错误.

该值是否可能实际上是一个包含值的列表?像 value=[1,2,3] 吗?在这种情况下,列表表示 GUI 中三个级别的 tabWidget 索引位置......或者这是不同的处理方式?欢迎所有/其他建议.

回溯错误:

<块引用>

createActionself.tabindex.valueChanged.connect(self.do_something)AttributeError: 'list' 对象没有属性 'valueChanged'

代码:

class Foo(QWidget):valueChanged = pyqtSignal(object)def __init__(self, parent=None):super(Foo, self).__init__(parent)self._t = [1,2,3]@财产def t(自我):返回 self._t@t.setterdef t(self, list):self._t = 列表self.valueChanged.emit(列表)def createAction(self):self.t.valueChanged.connect(self.do_something)def do_something(self):...print('在这里展示一些东西')

解决方案

valueChangedFoo 本身的信号,所以应该使用 self.valueChanged.connect(self.do_something) 而不是 self.t.valueChanged.connect(self.do_something).毕竟,应该与信号一起发射的值是在发射信号时确定的,而不是在将信号连接到插槽时确定的.

Based on SO question and answer given here I modified the answer where the value becomes a list. It throws me an error.

Is it possible that the value is in fact a list with values? Like value=[1,2,3]? In this case the list represents a tabWidget index positions at three levels in the GUI... Or is this handled differently? All/Other suggestions are welcome.

Traceback error:

createAction
    self.tabindex.valueChanged.connect(self.do_something)
AttributeError: 'list' object has no attribute 'valueChanged'

The code:

class Foo(QWidget):

    valueChanged = pyqtSignal(object)

    def __init__(self, parent=None):
        super(Foo, self).__init__(parent)
        self._t = [1,2,3]

    @property
    def t(self):
        return self._t

    @t.setter
    def t(self, list):
        self._t = list
        self.valueChanged.emit(list)

    def createAction(self):
        self.t.valueChanged.connect(self.do_something)

    def do_something(self):
        ...
        print('show something here')        

解决方案

valueChanged is a signal of Foo itself, so you should use self.valueChanged.connect(self.do_something) instead of self.t.valueChanged.connect(self.do_something). After all, the value that should be emitted with the signal is determined when the signal is emitted, not when connecting the signal to a slot.

这篇关于如何在具有值而不是单个变量值的列表上应用 PyQt5 事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆