mathomastech

Dynamic signal handling and button creating in PyQt4

May 21st, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. # Functioning example of how to dynamically generate buttons (or any element)
  2. # in PyQt4 and capture their emitted signals
  3. # Full code: www.github.com/mathomastech/HBS_Communicator
  4. # File of interest, handler.py
  5.  
  6. import functools
  7. from PyQt4 import QtGui, QtCore
  8. from gui import GUI
  9. from communicator import Communicator
  10.  
  11. class Handler(QtGui.QMainWindow):
  12.     def __init__(self):
  13.         super(Handler,self).__init__()
  14.     self.generate_channels()
  15.  
  16.     def generate_channels(self):
  17.             for i in range(0,len(Communicator.CHANNEL_LIST)):
  18.                 GUI.generate_channel_info(Communicator.CHANNEL_LIST[i][0],
  19.                                         Communicator.CHANNEL_LIST[i][1],
  20.                                         Communicator.CHANNEL_LIST[i][2],
  21.                                         Communicator.CHANNEL_LIST[i][3]
  22.                                      )
  23.  
  24.        
  25.             for i in range(0,len(GUI.CHANNELS)):
  26.                 self.button = QtGui.QPushButton(GUI.CHANNELS[i][GUI.CHANNEL_NAME])
  27.                 self.button.clicked.connect(functools.partial(self.channel_clicked,GUI.CHANNELS[i][GUI.CHANNEL_NAME]))
  28.                 GUI.CHANNELS[i][GUI.GUI_ELEMENT] = self.button
  29.                 self.button.setFlat(True)
  30.                 self.button.setSizePolicy(sizePolicy)
  31.                 self.button.setAutoDefault(True)
  32.                 if GUI.CHANNELS[i][GUI.CHANNEL_GROUP] == "command":
  33.                     self.com.commandLayout.addWidget(self.button)
  34.                 elif GUI.CHANNELS[i][GUI.CHANNEL_GROUP] == "general":
  35.                     self.com.generalLayout.addWidget(self.button)
  36.  
  37.     def channel_clicked(self,channel,flag):
  38.         Communicator.switch_channel(channel)
Advertisement
Add Comment
Please, Sign In to add comment