我對使用PyQt非常新,我試圖瞭解信號插槽機制。不幸的是,PyQt的文檔經常會導致Qt頁面的語法和參數幾乎不相同。我想在下面的簡單例子中找出2件事。 1)QAction :: triggered()是一個void函數,那麼我們如何在理論上由trigger()方法返回的某種對象上調用QAction :: triggered.connect()。PyQt5信號和插槽簡單代碼說明
2)什麼是「qApp」。我不知道QApp是什麼類型,或者它是由PyQt創建的,但似乎對我來說無處不在,只能在方便的時候使用。
我誤解的一部分可能來自這樣一個事實,即Qt/PyQt中函數的C++和python實現不相同,但我們希望能夠理解沒有任何python文檔的情況。
import sys
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication
from PyQt5.QtGui import QIcon
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
exitAction = QAction(QIcon('exit24.png'), 'Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.triggered.connect(qApp.quit)
self.toolbar = self.addToolBar('Exit')
self.toolbar.addAction(exitAction)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('Toolbar')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())