Python QoObject之间的PyQt信号
我试图在PyQt中创建一个视图和控制器,其中当单击按钮时视图会发出一个自定义信号,并且控制器的一个方法连接到发出的信号。然而,它不起作用。单击按钮时,不会调用respond方法。知道我做错了什么吗Python QoObject之间的PyQt信号,python,pyqt,signals,slot,Python,Pyqt,Signals,Slot,我试图在PyQt中创建一个视图和控制器,其中当单击按钮时视图会发出一个自定义信号,并且控制器的一个方法连接到发出的信号。然而,它不起作用。单击按钮时,不会调用respond方法。知道我做错了什么吗 import sys from PyQt4.QtCore import * from PyQt4.QtGui import QPushButton, QVBoxLayout, QDialog, QApplication class TestView(QDialog): def __init
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import QPushButton, QVBoxLayout, QDialog, QApplication
class TestView(QDialog):
def __init__(self, parent=None):
super(TestView, self).__init__(parent)
self.button = QPushButton('Click')
layout = QVBoxLayout()
layout.addWidget(self.button)
self.setLayout(layout)
self.connect(self.button, SIGNAL('clicked()'), self.buttonClicked)
def buttonClicked(self):
self.emit(SIGNAL('request'))
class TestController(QObject):
def __init__(self, view):
self.view = view
self.connect(self.view, SIGNAL('request'), self.respond)
def respond(self):
print 'respond'
app = QApplication(sys.argv)
dialog = TestView()
controller = TestController(dialog)
dialog.show()
app.exec_()
适用于我-可能是您正在使用的Qt/PyQt版本,但有几件事您可以尝试:
import sys
from PyQt4.QtCore import QObject, pyqtSignal # really shouldn't import * here...QtCore library is quite large
from PyQt4.QtGui import QPushButton, QVBoxLayout, QDialog, QApplication
class TestView(QDialog):
request = pyqtSignal()
def __init__(self, parent=None):
super(TestView, self).__init__(parent)
self.button = QPushButton('Click')
layout = QVBoxLayout()
layout.addWidget(self.button)
self.setLayout(layout)
self.button.clicked.connect(self.buttonClicked)
def buttonClicked(self):
self.request.emit()
class TestController(QObject):
def __init__(self, view):
super(QObject, self).__init__()
self.view = view
self.view.request.connect(self.respond)
def respond(self):
print 'respond'
app = QApplication(sys.argv)
dialog = TestView()
controller = TestController(dialog)
dialog.show()
app.exec_()
同样,我非常非常不鼓励以这种方式构建代码……您正在创建大量不必要的工作,并在不需要的时候复制对象。对我有效-可能是您正在使用的Qt/PyQt版本,但有几件事您可以尝试:
import sys
from PyQt4.QtCore import QObject, pyqtSignal # really shouldn't import * here...QtCore library is quite large
from PyQt4.QtGui import QPushButton, QVBoxLayout, QDialog, QApplication
class TestView(QDialog):
request = pyqtSignal()
def __init__(self, parent=None):
super(TestView, self).__init__(parent)
self.button = QPushButton('Click')
layout = QVBoxLayout()
layout.addWidget(self.button)
self.setLayout(layout)
self.button.clicked.connect(self.buttonClicked)
def buttonClicked(self):
self.request.emit()
class TestController(QObject):
def __init__(self, view):
super(QObject, self).__init__()
self.view = view
self.view.request.connect(self.respond)
def respond(self):
print 'respond'
app = QApplication(sys.argv)
dialog = TestView()
controller = TestController(dialog)
dialog.show()
app.exec_()
同样,我非常非常不鼓励以这种方式构建代码……您正在创建大量不必要的工作,并在不需要的时候复制对象。与此同时,我发现我应该在我的TestController的init中调用QObject的init:QObject。uu init_uu(self)谢谢您的建议,Eric,非常感谢。如果我理解正确,你会鼓励在TestView的buttonClicked方法中直接处理按钮单击?哦,是的,我错过了这个-这很可能是导致问题的原因…奇怪的是,它在我的测试中起了作用,可能对我来说也应该失败。我以为你是问这个问题的同一个人,因为它几乎完全一样:同时,我发现我应该在我的TestController:QObject的init中调用QObject的init。谢谢你的建议,Eric,非常感谢。如果我理解正确,你会鼓励在TestView的buttonClicked方法中直接处理按钮单击?哦,是的,我错过了这个-这很可能是导致问题的原因…奇怪的是,它在我的测试中起了作用,可能对我来说也应该失败。我以为你是问这个问题的同一个人,因为这个问题几乎完全一样: