title: Using a delegate with a QDataWidgetMapper and QLabel
body: <p>I'm trying to use a delegate to customize the way data from a model is displayed when using a QDataWidgetMapper.</p>
  
  <p>I have two different versions of a widget, one is view-only (the data is displayed in QLabels) and the other is used to edit the data (the data is displayed in appropriate editors).</p>
  
  <p>The latter one works perfectly with the delegate, everything is fine.</p>
  
  <p>As you may have guessed the problem arises with the first one... When mapping the sections of my model to QLabels using the QDataWidgetMapper, the delegate is never called and the data is displayed correctly for the sections with regular data (strings, ints,...) but no data is displayed for the sections of my model with a custom data type (a kind of list) which I would like to format as a string using the delegate.</p>
  
  <p>I've already performed this operation successfully when the same data is displayed in a QTableView (the method paint() of the delegate is called when the data is displayed).</p>
  
  <p>After having looked at it a little bit closer, I've been able to see that, when using QLabels to display the data, the delegate is never called though I've explicitly associated a delegate to the QDataWidgetMapper using its method setItemDelegate().</p>
  
  <p>So in synthesis, assume a class CustomItemDelegate which inherits QStyledItemDelegate with virtual methods:</p>
  
  <pre><code>void CustomItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) const {
      qDebug() &lt;&lt; "DELEGATE: PAINT" &lt;&lt; index.column();
      QStyledItemDelegate::paint(painter, option, index);
  }
  
  void CustomItemDelegate::setEditorData(QWidget *editor, const QModelIndex &amp;index) const {
      qDebug() &lt;&lt; "DELEGATE: SET EDITOR DATA" &lt;&lt; index.column();
      QStyledItemDelegate::setEditorData(editor, index);
  }
  </code></pre>
  
  <p>and a widget with following in it:</p>
  
  <pre><code>QDataWidgetMapper* mapper = new QDataWidgetMapper();
  CustomItemDelegate* delegate = new CustomItemDelegate();
  mapper-&gt;setModel(model);
  mapper-&gt;setItemDelegate(delegate);
  
  mapper-&gt;addMapping(editorWidget, 1);
  mapper-&gt;addMapping(label, 2, "text");
  
  mapper-&gt;toFirst();
  
  QTableView* view = new QTableView();
  CustomItemDelegate* delegate2 = new CustomItemDelegate();
  view-&gt;setModel(model);
  view-&gt;setItemDelegate(delegate2);
  </code></pre>
  
  <p>the code outputs:</p>
  
  <pre><code>DELEGATE: SET EDITOR DATA 1
  // NOTHING ?!
  
  DELEGATE: PAINT 1
  DELEGATE: PAINT 2
  </code></pre>
  
  <p>and as a result I got </p>
  
  <ul>
  <li>my editorWidget with the correct data in it (whatever data type the section contains: regular or custom, as long as the editor handles the type of course), </li>
  <li>my label only displays the data if the section contains a regular type of the data as the delegate is not called</li>
  <li>my view would display everything fine as the delegate is called for each section</li>
  </ul>
  
  <p>So my questions are:</p>
  
  <ul>
  <li>why isn't the delegate called when the mapped widget is a QLabel?</li>
  <li>in this case, how come the data is even displayed when the data type is regular? Magic?</li>
  </ul>
  
  <p>Thanks very much and I apologize in advance if the answer is obvious (but even then, thank you for pointing it out :P),</p>
  
  <p>ixM</p>