连接自定义按钮的clicked()信号

时间:2017-12-03 16:08:09

标签: python pyqt pyqt5

我刚刚从PyQt4升级到PyQt5,这是我想要做的,这应该很简单:

我正在建立一个按钮列表,并希望每个按钮发出“点击”信号,将一个号码发送到一个独特的功能中:

from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import QObject, pyqtSlot

class ButtonList(QObject):

  def __init__(self, titles):
     super(ButtonList, self).__init__()                                                                                         

     self._list = []                                                                                                                    

     for title in titles:
       btn = CustomButton(title, titles.index(title))                                                                                                                                                                                                                  
       btn._clicked.connect(self.on_clicks)
       self._list.append(btn)                                                                                                                                                                                                                     

  @pyqtSlot(int)                                                                                                                       
  def on_clicks(self, index):
      print("test")

class CustomButton(QPushButton):
   _clicked = pyqtSignal(int)

   def __init__(self, text, index, parent=None):
       super(CustomButton, self).__init__()                                                                                                        
       self.setText(text)
       self.index = index
       self.setCheckable(True)
       self.clicked.connect(self._on_clicks)

   def _on_clicks(self, clicked):
       self._clicked.emit(self.index)
       print("{:d} is emitting".format(self.index))

所以我确实看到了排放,但处理程序从未被调用

0 个答案:

没有答案