Signal + slot test case for pyqt5

When writing code with pycharm, it was found that the signal set a slot for connection, but the test found that it did not work. In addition, without...


When writing code with pycharm, it was found that the signal set a slot for connection, but the test found that it did not work. In addition, without this function, the connection prompt on the pycharm Editor could not always find the reason, so it was decided later that the corresponding test cases needed to be learned and verified when encountering the corresponding knowledge points.

Below is a typical example of a signal + slot.It works as expected and connect s in pycharm shows that there is no such function. It should be a problem set and prompted by pycharm itself, which is ignored later.So sometimes you can't drill down to the top, pycharm is just an editor.


#-*- coding:utf-8 -*- ''' Signal & Slot ''' __author__ = 'Tony Zhu' import sys from PyQt5.QtCore import Qt from PyQt5.QtWidgets import (QWidget, QLCDNumber, QSlider,QGridLayout,QLabel,QHBoxLayout, QGroupBox, QVBoxLayout, QApplication,QProgressBar,QPushButton,QMessageBox) class SignalSlot(QWidget): def __init__(self): super(SignalSlot,self).__init__() self.initUI() def initUI(self): self.controlsGroup = QGroupBox("Running Samples") self.lcdNumber = QLCDNumber(self) self.slider = QSlider(Qt.Horizontal, self) self.pBar = QProgressBar(self) vbox = QVBoxLayout() vbox.addWidget(self.pBar) vbox.addWidget(self.lcdNumber) vbox.addWidget(self.slider) self.controlsGroup.setLayout(vbox) controlsLayout = QGridLayout() self.label1 = QLabel("Save state:") self.saveLabel = QLabel() self.label2 = QLabel("Running state:") self.runLabel = QLabel() self.buttonSave = QPushButton("Preservation") self.buttonRun = QPushButton("Function") self.buttonStop = QPushButton("Stop it") self.buttonDisconnect = QPushButton("Disassociation") self.buttonConnect = QPushButton("Binding Association") controlsLayout.addWidget(self.label1,0,0) controlsLayout.addWidget(self.saveLabel,0,1) controlsLayout.addWidget(self.label2,1,0) controlsLayout.addWidget(self.runLabel,1,1) controlsLayout.addWidget(self.buttonSave,2,0) controlsLayout.addWidget(self.buttonRun,2,1) controlsLayout.addWidget(self.buttonStop,2,2) controlsLayout.addWidget(self.buttonDisconnect,3,0) controlsLayout.addWidget(self.buttonConnect,3,1) layout = QHBoxLayout() layout.addWidget(self.controlsGroup) layout.addLayout(controlsLayout) self.setLayout(layout) self.buttonRun.clicked.connect(self.buttonSave.clicked) self.slider.valueChanged.connect(self.pBar.setValue) self.slider.valueChanged.connect(self.lcdNumber.display) self.buttonSave.clicked.connect(self.showMessage) self.buttonRun.clicked.connect(self.showMessage) self.buttonDisconnect.clicked.connect(self.unbindConnection) self.buttonConnect.clicked.connect(self.bindConnection) self.buttonStop.clicked.connect(self.stop) self.setGeometry(300, 500, 500, 180) self.setWindowTitle('Signals and Slots') def showMessage(self): if self.sender().text() == "Preservation": self.saveLabel.setText("Saved") elif self.sender().text() == "Function": self.saveLabel.setText("Saved") self.runLabel.setText("Running") def unbindConnection(self): self.slider.valueChanged.disconnect() def bindConnection(self): self.slider.valueChanged.connect(self.pBar.setValue) self.slider.valueChanged.connect(self.lcdNumber.display) def stop(self): self.saveLabel.setText("") self.runLabel.setText("") if __name__ == '__main__': app = QApplication(sys.argv) ex = SignalSlot() ex.show() sys.exit(app.exec_())


7 November 2019, 09:02 | Views: 1776

Add new comment

For adding a comment, please log in
or create account

0 comments