Advertisement
robertvari

customButton

Mar 6th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. from PySide2.QtWidgets import QPushButton, QWidget, QApplication, QVBoxLayout
  2. from PySide2.QtCore import Signal
  3. import sys
  4.  
  5. class Window(QWidget):
  6.     def __init__(self):
  7.         super(Window, self).__init__()
  8.         mainlayout = QVBoxLayout(self)
  9.  
  10.         button = MyButton("Test string")
  11.         button.clickAction.connect(self.buttonAction) # catch our signal
  12.         mainlayout.addWidget(button)
  13.  
  14.     def buttonAction(self, buttonText): # function gets the signal string by default
  15.         print(buttonText)
  16.  
  17.  
  18. class MyButton(QPushButton):
  19.     clickAction = Signal(str) # signal prepared for button text
  20.  
  21.     def __init__(self, buttonText):
  22.         super(MyButton, self).__init__()
  23.         self.setText(buttonText)
  24.  
  25.         self.clicked.connect(self.emmitMySignal)
  26.  
  27.     def emmitMySignal(self):
  28.         self.clickAction.emit(self.text()) # emit signal with text
  29.  
  30. app = QApplication(sys.argv)
  31. win = Window()
  32. win.show()
  33. app.exec_()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement