将QListView的双击事件与PyQt4中的方法连接
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它似乎有效,如果:
被替换为新语法:
后者也更加优雅。然而,我仍然不知道为什么原始语法不起作用。
It seems to work if:
Is replaced with the new syntax of:
The latter is much more elegant too. I still do not know why the original syntax did not work, however.
如果您使用以下方法,它也将起作用:
检查 pyqt 参考,然后按原样复制并粘贴信号。
我知道你已经解决了。但我认为了解不止一种方法会更好。
It will also work if you use:
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.
itemDoubleClicked
是由QListWidget
而不是QListView
发出的信号。我测试了 Moayyad Yaghi 的建议,它至少在带有 python 2.5 的 Qt 4 上对我不起作用,但是 lb.doubleClicked.connect(self.someMethod) 工作得很好。
itemDoubleClicked
is a signal emitted byQListWidget
and notQListView
. I tested Moayyad Yaghi's suggestion and it did not work for me at least on Qt 4 with python 2.5Though,
lb.doubleClicked.connect(self.someMethod)
works perfectly fine.