title: Displaying multiple icons in a single cell of a QTableView
body: <p>I am writing a small gui app with QT4.5 in QtCreator.</p>
  
  <p>The main screen on the app contains a QTreeView with two columns, the first is text the second is a group of icons.  These icons represent the last few states of the item displayed in the row.</p>
  
  <p>I am not sure what the best way to do this is.  I have currently implemented this by generating a QPixmap the model's <code>data()</code> method.</p>
  
  <pre><code>QVariant MyModel::data(const QModelIndex &amp;index, int role) const
  {
      if (role == Qt::DisplayRole || role == Qt::EditRole) {
          switch(index.column()) {
              case 0:
                  return item_.at(index.row()).title();
          }
      }
      if (role == Qt::DecorationRole) {
          switch(index.column()) {
              case 1:
                  return makeImage(item_.add(index.row()).lastStates());
          }
      }
  
      return QVariant();
  }
  
  QVariant MyModel::makeImage(const QList&lt;MyState&gt; &amp;states) const
  {
      const int IconSize = 22;
      QPixmap image(IconSize * states.size(), IconSize);
      QPainter painter(&amp;image);
  
      painter.fillRect(0, 0, IconSize * count, IconSize, Qt::transparent);
      for (int i = 0; i &lt; states.size(); ++i) {
          QIcon * icon = stateIcon(state.at(i));
          icon-&gt;paint(&amp;painter, IconSize * i, 0, IconSize, IconSize);
      }
      return image;
  }
  </code></pre>
  
  <p>This works but for some small problems, the background which should be transparent is full of random noise, even filling this with a transparent colour does not fix it.</p>
  
  <p>Second this does not seem very efficient, I am generating a new Image every time this is called, should I not just draw the icons onto the widget for the cell?</p>
  
  <p>What is the best way to display multiple icons in a single cell?</p>