将QListView的双击事件与PyQt4中的方法连接

发布于 2024-10-08 19:40:50 字数 658 浏览 0 评论 0原文

我有一个 PyQt QListView 对象,我想要一个在双击它时运行的方法。这应该是微不足道的,但它似乎不起作用。我的代码如下:

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        lb = QListView()
        self.connect(lb, SIGNAL('doubleClicked()'), self.someMethod)

        grid = QGridLayout()
        grid.addWidget(lb, 0, 0)
        centralWidget.setLayout(grid)

    def someMethod(self):
        print "It happened!"

我也尝试过 clicked()entered() 方法,但它们也不起作用。这些事件均在此处的文档中列出。

I’ve got a PyQt QListView object, and I want a method to run when it is double-clicked. This should be trivial, but it doesn't seem to work. My code is as follows:

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        lb = QListView()
        self.connect(lb, SIGNAL('doubleClicked()'), self.someMethod)

        grid = QGridLayout()
        grid.addWidget(lb, 0, 0)
        centralWidget.setLayout(grid)

    def someMethod(self):
        print "It happened!"

I’ve tried clicked() and entered() methods too, but they do not work either. These events are all listed in the documentation here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

夏尔 2024-10-15 19:40:50

它似乎有效,如果:

self.connect(lb, SIGNAL('doubleClicked()'), self.someMethod)

被替换为新语法:

lb.doubleClicked.connect(self.someMethod)

后者也更加优雅。然而,我仍然不知道为什么原始语法不起作用。

It seems to work if:

self.connect(lb, SIGNAL('doubleClicked()'), self.someMethod)

Is replaced with the new syntax of:

lb.doubleClicked.connect(self.someMethod)

The latter is much more elegant too. I still do not know why the original syntax did not work, however.

老子叫无熙 2024-10-15 19:40:50

如果您使用以下方法,它也将起作用:

self.connect(lb,QtCore.SIGNAL("itemDoubleClicked (QListWidgetItem *)"),self.someMethod)

检查 pyqt 参考,然后按原样复制并粘贴信号。

我知道你已经解决了。但我认为了解不止一种方法会更好。

It will also work if you use:

self.connect(lb,QtCore.SIGNAL("itemDoubleClicked (QListWidgetItem *)"),self.someMethod)

check the pyqt reference, then copy and paste the signal as is.

I know you already solved it. but I think knowing more than one method will be better.

鸵鸟症 2024-10-15 19:40:50

itemDoubleClicked 是由 QListWidget 而不是 QListView 发出的信号。我测试了 Moayyad Yaghi 的建议,它至少在带有 python 2.5 的 Qt 4 上对我不起作用

,但是 lb.doubleClicked.connect(self.someMethod) 工作得很好。

itemDoubleClicked is a signal emitted by QListWidget and not QListView. I tested Moayyad Yaghi's suggestion and it did not work for me at least on Qt 4 with python 2.5

Though, lb.doubleClicked.connect(self.someMethod) works perfectly fine.

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