将 PyQt4 QActiontriggered() 绑定到本地类可调用似乎不起作用。如何调试这个?

发布于 2024-08-29 10:54:51 字数 627 浏览 6 评论 0原文

当我想创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

飘过的浮云 2024-09-05 10:54:51

也许有点晚了,但我遇到了同样的问题,我通过更改解决了它:
类 ActionObject(对象)

类 ActionObject()

Maybe a bit late but I had the same problem and I solved it by changing:
class ActionObject(object)
to
class ActionObject()

一紙繁鸢 2024-09-05 10:54:51

我没有看到您将 action 添加到此代码中的任何菜单(事实上,我在任何地方都没有看到 addAction 调用),并且有一个变量 checked 的特殊用法 从未在您的 triggered 方法中的任何地方定义过(它是在其他地方定义的全局变量吗?)。这两个问题都表明您有其他未显示的代码(将该操作添加到某些菜单或工具栏并定义全局变量 checked 的代码 - 或者您是否在def 触发 语句,也许...?),这就是错误所在(此代码是潜在正确程序的子集...但显然存在缺少部分,我们如何知道它们是正确的?-)。

I don't see you adding action to any menu in this code (indeed I see no addAction call anywhere), and there's a peculiar use of a variable checked that is never defined anywhere in your triggered 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 variable checked -- or did you omit a parameter in the def 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?-).

驱逐舰岛风号 2024-09-05 10:54:51

看起来您的调试必须发生在您未提供的两个类之一中;您将属性附加到它们,然后将它们作为参数传递给 ActionObject。

我创建了一个没有这个的例子,因为我不知道你的其他两个类是什么样的。第三个,父类,我不需要,因为它当然可以是任何继承了QWidget/QMainWindow/QDialog等的泛型类

class ActionObject(object):
  def __init__(self, owner=None, command=None):
    self.menuBar = QtGui.QMenuBar(self)
    self.setMenuBar(self.menuBar)
    # Make sure the menu item's parent is the menu
    self.menuGeneric = QtGui.QMenu(self.menuBar)
    self.menuGeneric.setTitle('&Generic')
    # Create and add the action in one line
    self.menuBar.addAction(self.menuGeneric.menuAction())
    QtCore.QObject.connect(self.menuGeneric, qc.SIGNAL('triggered()'), self.triggered)
  def triggered(self):
    print "got triggered"

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

class ActionObject(object):
  def __init__(self, owner=None, command=None):
    self.menuBar = QtGui.QMenuBar(self)
    self.setMenuBar(self.menuBar)
    # Make sure the menu item's parent is the menu
    self.menuGeneric = QtGui.QMenu(self.menuBar)
    self.menuGeneric.setTitle('&Generic')
    # Create and add the action in one line
    self.menuBar.addAction(self.menuGeneric.menuAction())
    QtCore.QObject.connect(self.menuGeneric, qc.SIGNAL('triggered()'), self.triggered)
  def triggered(self):
    print "got triggered"
如梦初醒的夏天 2024-09-05 10:54:51

对于你的两个问题:

1) 我该如何调试这个?

1)我要尝试的第一件事是看看你的函数的参数声明是否错误(你有)。为此,我将 *args**kwargs 添加到您的函数中,然后运行代码以查看它是否有效:

def triggered(self, *args, **kwargs):
    print("got triggered " + self.command.id + " " + repr(checked) + " [extra args: {}, kwargs: {}".format(args, kwargs))

我打赌您会发现您是获取一个布尔值作为函数的第一个参数,但您的函数被声明为不接受任何参数。异常可能是被记录到 stderr 或被吞噬。

2)为了方便起见,我创建了一个简单的装饰器来记录这些类型的内容:

import functools, sys
def logged_action(func):
    @functools.wraps(func)
    def wrapped(*args, **kwargs):
        sys.stderr.write("{}: {}, {}\n".format(func, args, kwargs))
        return func(*args, **kwargs)
    return wrapped

@logged_action
def triggered(self, *args, **kwargs):
    print("got triggered " + self.command.id + " " + repr(checked))

2)(或者,我做错了什么)

根据我必须提供的示例的经验,您连接的方法没有正确的签名:

Traceback (most recent call last):
  File "[redacted]", line 12, in wrapped
    return func(*args, **kwargs)
TypeError: triggered() takes exactly 1 argument (2 given)

使用 self 和另一个参数调用 triggered (因此“给出了 2 个”),但你只是声明你拿了一个。

For your two questions:

1) How do I debug this?

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:

def triggered(self, *args, **kwargs):
    print("got triggered " + self.command.id + " " + repr(checked) + " [extra args: {}, kwargs: {}".format(args, kwargs))

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:

import functools, sys
def logged_action(func):
    @functools.wraps(func)
    def wrapped(*args, **kwargs):
        sys.stderr.write("{}: {}, {}\n".format(func, args, kwargs))
        return func(*args, **kwargs)
    return wrapped

@logged_action
def triggered(self, *args, **kwargs):
    print("got triggered " + self.command.id + " " + repr(checked))

2) (or, what am I doing wrong)

Your connected method doesn't have the right signature, based on my experience with an example I had to hand:

Traceback (most recent call last):
  File "[redacted]", line 12, in wrapped
    return func(*args, **kwargs)
TypeError: triggered() takes exactly 1 argument (2 given)

triggered is being called with self and another argument (hence "2 given"), but you're only declaring you take one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文