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

Python PyQt5:未调用单独文件中的插槽,python,pyqt,pyqt5,qt-signals,qt-slot,Python,Pyqt,Pyqt5,Qt Signals,Qt Slot,我现在有一个基本的GUI,每个页面都有自己的文件。我可以毫无问题地浏览每个页面,但我很难简单地将搜索查询传递给另一个小部件。以下是我在主文件中设置连接的位置: from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * import sys import search import watching import helpinfo import results class MainWind

我现在有一个基本的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,将屏幕更改为results屏幕(按预期工作)。第二个,passQuery,应该将文本框的内容传递给结果文件中连接的函数grabSearch()。然而,尽管已连接,passQuery似乎从未被结果页捕获。我已经用print语句验证了它正在被释放,但仅此而已


我在这里遗漏了什么?

您的代码有以下错误:

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

但最好使用直接连接,因为签名相同:

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

    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) # <--
    
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) # <--
@pyqtSlot(str)
def grabSearch(self, text):
    self.label.setText(text)