PyQt5:单独文件中的插槽未被调用

PyQt5: Slot in separate file not being called

我目前有一个基本的 GUI,每个页面都在自己的文件中。我可以毫无问题地往返于每个页面,但我很难简单地将搜索查询传递给另一个小部件。这是我在主文件中设置连接的地方:

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
import search
import watching
import helpinfo
import results

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        '''
        Constructor
        '''
        QMainWindow.__init__(self, parent)
        self.centralWidget = QStackedWidget()
        self.setCentralWidget(self.centralWidget)
        self.startScreen = Start(self)
        self.searchScreen = search.Search(self)
        self.watchingScreen = watching.Watching(self)
        self.helpInfoScreen = helpinfo.HelpInfo(self)
        self.resultsScreen = results.Results(self)
        self.centralWidget.addWidget(self.startScreen)
        self.centralWidget.addWidget(self.searchScreen)
        self.centralWidget.addWidget(self.watchingScreen)
        self.centralWidget.addWidget(self.helpInfoScreen)
        self.centralWidget.addWidget(self.resultsScreen)
        self.centralWidget.setCurrentWidget(self.startScreen)

        self.startScreen.searchClicked.connect(lambda: self.centralWidget.setCurrentWidget(self.searchScreen))
        self.startScreen.watchingClicked.connect(lambda: self.centralWidget.setCurrentWidget(self.watchingScreen))
        self.startScreen.helpInfoClicked.connect(lambda: self.centralWidget.setCurrentWidget(self.helpInfoScreen))

        self.searchScreen.searchSubmitted.connect(lambda: self.centralWidget.setCurrentWidget(self.resultsScreen))
        self.searchScreen.passQuery.connect(lambda: self.resultsScreen.grabSearch) #This is the problem line

        self.searchScreen.clicked.connect(lambda: self.centralWidget.setCurrentWidget(self.startScreen))
        self.watchingScreen.clicked.connect(lambda: self.centralWidget.setCurrentWidget(self.startScreen))
        self.helpInfoScreen.clicked.connect(lambda: self.centralWidget.setCurrentWidget(self.startScreen))
        self.resultsScreen.clicked.connect(lambda: self.centralWidget.setCurrentWidget(self.startScreen))  

这是搜索文件:

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys

class Search(QWidget):

    clicked = pyqtSignal()
    searchSubmitted = pyqtSignal()
    passQuery = pyqtSignal(str)

    def __init__(self, parent=None):
        super(Search, self).__init__(parent)

        logo = QLabel(self)
        pixmap = QPixmap('res/logo.png')
        logo.setPixmap(pixmap)
        logo.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
        logo.setAlignment(Qt.AlignCenter)

        self.textbox = QLineEdit(self)

        label = QLabel(text="This is the search page.")
        label.setAlignment(Qt.AlignCenter)
        button = QPushButton(text='Submit')
        button.clicked.connect(lambda: self.submitSearch())
        button2 = QPushButton(text='Go back.')
        button2.clicked.connect(self.clicked.emit)

        layout = QVBoxLayout()        
        layout.addWidget(logo)
        layout.addWidget(label)
        layout.addWidget(self.textbox)
        layout.addWidget(button)
        layout.addWidget(button2)
        layout.setAlignment(Qt.AlignTop)
        self.setLayout(layout)

    def submitSearch(self):
        self.searchSubmitted.emit()
        self.passQuery.emit(self.textbox.text())

这是结果文件:

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class Results(QWidget):

    clicked = pyqtSignal()

    def __init__(self, parent=None):
        super(Results, self).__init__(parent)

        # Create Logo
        logo = QLabel(self)
        pixmap = QPixmap('res/logo.png')
        logo.setPixmap(pixmap)
        logo.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
        logo.setAlignment(Qt.AlignCenter)


        # Create page contents
        label = QLabel(text="This is the results page. If you see this, it's still broken.")
        label.setAlignment(Qt.AlignCenter)
        button = QPushButton(text='Add to watching.')
        button2 = QPushButton(text='Go back.')
        button2.clicked.connect(self.clicked.emit)

        # Set up layout
        layout = QVBoxLayout()
        layout.addWidget(logo)
        layout.addWidget(label)
        layout.addWidget(button)
        layout.addWidget(button2)
        layout.setAlignment(Qt.AlignTop)

        self.setLayout(layout)

    @pyqtSlot(str)
    def grabSearch(self, str):
        print(str)
        self.label.setText(str)

按照我的理解,我现在所拥有的应该是有效的。当用户在搜索页面上提交一些文本时,它会调用 submitSearch() 函数。该函数发出两个信号:第一个是 searchSubmitted,将屏幕更改为结果屏幕(按预期工作)。第二个,passQuery,应该将文本框的内容传递给结果文件中连接的函数 grabSearch()。但是,尽管已连接,但 passQuery 似乎从未被结果页面捕获。我已经用打印语句验证了它 被发出,但仅此而已。

我在这里错过了什么?

您的代码存在以下错误:

  • 如果您要使用 lambda 建立连接,则必须使用参数调用该函数。

self.searchScreen.passQuery.connect(lambda text: self.resultsScreen.grabSearch(text))

但由于签名相同,最好使用直接连接:

self.searchScreen.passQuery.connect(self.resultsScreen.grabSearch)
  • 另一个错误是results.py标签必须是class的成员:

    self.label = QLabel(text="This is the results page. If you see this, it's still broken.") # <-- 
    self.label.setAlignment(Qt.AlignCenter) # <--
    # ..
    
    # Set up layout
    layout = QVBoxLayout()
    layout.addWidget(logo)
    layout.addWidget(self.label) # <--
    
  • 最后不要用str这样的保留字,改成:

    @pyqtSlot(str)
    def grabSearch(self, text):
        self.label.setText(text)