def list_legend(model, selected=None):
 if hasattr(model, 'model'):
  model = model.model()
 legend = ''
 for row in range(model.rowCount()):
  if ((selected is not None) and (row not in selected)):
   continue
  index = model.index(row, 0)
  icon = model.data(index, Qt.DecorationRole)
  (r, g, b, a) = QColor(icon.pixmap(12, 12).toImage().pixel(0, 0)).getRgb()
  text = model.data(index, Qt.DisplayRole)
  legend += (colored_square(r, g, b) + '<span class="legend-item">{}</span>'.format(text))
 return legend

def list_legend ( model , selected = None ) : if hasattr ( model , 'model' ) : model = model . model ( ) legend = '' for row in range ( model . rowCount ( ) ) : if ( ( selected is not None ) and ( row not in selected ) ) : continue index = model . index ( row , 0 ) icon = model . data ( index , Qt . DecorationRole ) ( r , g , b , a ) = QColor ( icon . pixmap ( 12 , 12 ) . toImage ( ) . pixel ( 0 , 0 ) ) . getRgb ( ) text = model . data ( index , Qt . DisplayRole ) legend += ( colored_square ( r , g , b ) + '<span class="legend-item">{}</span>' . format ( text ) ) return legend
create html with a legend constructed from a qt model or a view .