将 PyQt4 QActiontriggered() 绑定到本地类可调用似乎不起作用。如何调试这个?
当我想创建 QAction 时,我创建了这个对象。然后我将此 QAction 添加到菜单中:
class ActionObject(object):
def __init__(self, owner, command):
action = QtGui.QAction(command.name, owner)
self.action = action
self.command = command
action.setShortcut(command.shortcut)
action.setStatusTip(command.name)
QtCore.QObject.connect(action, QtCore.SIGNAL('triggered()'), self.triggered)
def triggered(self):
print("got triggered " + self.command.id + " " + repr(checked))
不幸的是,当选择菜单项时,不会调用“触发”函数。 QtCore.QObject.connect() 返回 True。 控制台上不会打印任何内容来表明出现任何问题,也不会引发任何异常。
我该如何调试这个? (或者,我做错了什么?)
I create this object when I want to create a QAction. I then add this QAction to a menu:
class ActionObject(object):
def __init__(self, owner, command):
action = QtGui.QAction(command.name, owner)
self.action = action
self.command = command
action.setShortcut(command.shortcut)
action.setStatusTip(command.name)
QtCore.QObject.connect(action, QtCore.SIGNAL('triggered()'), self.triggered)
def triggered(self):
print("got triggered " + self.command.id + " " + repr(checked))
Unfortunately, when the menu item is selected, the 'triggered' function is not called.
QtCore.QObject.connect() returns True.
Nothing is printed on the console to indicate that anything is wrong, and no exception is thrown.
How can I debug this? (or, what am I doing wrong?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许有点晚了,但我遇到了同样的问题,我通过更改解决了它:
类 ActionObject(对象)
到
类 ActionObject()
Maybe a bit late but I had the same problem and I solved it by changing:
class ActionObject(object)
to
class ActionObject()
我没有看到您将
action
添加到此代码中的任何菜单(事实上,我在任何地方都没有看到addAction
调用),并且有一个变量checked 的特殊用法
从未在您的triggered
方法中的任何地方定义过(它是在其他地方定义的全局变量吗?)。这两个问题都表明您有其他未显示的代码(将该操作添加到某些菜单或工具栏并定义全局变量checked
的代码 - 或者您是否在def 触发
语句,也许...?),这就是错误所在(此代码是潜在正确程序的子集...但显然存在缺少部分,我们如何知道它们是正确的?-)。I don't see you adding
action
to any menu in this code (indeed I see noaddAction
call anywhere), and there's a peculiar use of a variablechecked
that is never defined anywhere in yourtriggered
method (is it a global defined elsewhere?). Both of these issues suggest you have other code that you're not showing (the code that adds this action to some menu or toolbar and defines the global variablechecked
-- or did you omit a parameter in thedef triggered
statement, perhaps...?), and that would be where the bugs lie (this code is a subset of a potentially correct program... but clearly there are missing parts, and how do we know they are correct?-).看起来您的调试必须发生在您未提供的两个类之一中;您将属性附加到它们,然后将它们作为参数传递给 ActionObject。
我创建了一个没有这个的例子,因为我不知道你的其他两个类是什么样的。第三个,父类,我不需要,因为它当然可以是任何继承了QWidget/QMainWindow/QDialog等的泛型类
Looks like your debugging has to occur in one of the two classes you don't provide; where you're attaching attributes to them then passing them to ActionObject as parameters.
I've created an example without this, as I have no idea what your other two classes look like. The third, parent class, I don't need, because of course it can be any generic Class that has inherited QWidget/QMainWindow/QDialog, etc
对于你的两个问题:
1)我要尝试的第一件事是看看你的函数的参数声明是否错误(你有)。为此,我将
*args
和**kwargs
添加到您的函数中,然后运行代码以查看它是否有效:我打赌您会发现您是获取一个布尔值作为函数的第一个参数,但您的函数被声明为不接受任何参数。异常可能是被记录到 stderr 或被吞噬。
2)为了方便起见,我创建了一个简单的装饰器来记录这些类型的内容:
根据我必须提供的示例的经验,您连接的方法没有正确的签名:
使用 self 和另一个参数调用
triggered
(因此“给出了 2 个”),但你只是声明你拿了一个。For your two questions:
1) First thing I'd try would be to see if you've got the argument declaration for your function wrong (you have). To do that, I'd add
*args
and**kwargs
to your function and then run the code to see if it works:I bet you'll find you were getting a boolean as the first argument to the function, but your function was declared as not taking any. The exception was possibly getting logged to stderr or being swallowed.
2) I created a simple decorator to log these types of things for convenience:
Your connected method doesn't have the right signature, based on my experience with an example I had to hand:
triggered
is being called with self and another argument (hence "2 given"), but you're only declaring you take one.