title: Drawing QTreeView items with QStyledItemDelegate
body: <p>I have created a project based on the <code>Simple Tree Model Example</code> from the Qt examples list and edited its model so that some items can also be checked; this is how it looks like:</p>
  
  <p><a href="https://i.stack.imgur.com/IgOya.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IgOya.png" alt="enter image description here"></a></p>
  
  <p>I would like some of the items to be slightly moved to the left. So I created a delegate by deriving <code>QStyledItemDelegate</code> and reimplemented <code>QStyledItemDelegate::paint</code> like this:</p>
  
  <pre><code>void TreeItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) const
  {
      if (index.column() == 0) {
          TreeItem *childItem = static_cast&lt;TreeItem*&gt;(index.internalPointer());
          if (childItem-&gt;type() == TreeItem::Type::Checkable) {
              QStyleOptionViewItem opt = option;
              QStyledItemDelegate::initStyleOption(&amp;opt, index);
  
              const int desiredThreshold = 10;
  
              opt.rect.setX(opt.rect.x() - desiredThreshold);
              option.widget-&gt;style()-&gt;drawControl(QStyle::CE_ItemViewItem, &amp;opt, painter, option.widget);
  
              return;
          }
      }
  
      return QStyledItemDelegate::paint(painter, option, index);
  }
  </code></pre>
  
  <p>This way items of type <code>TreeItem::Type::Checkable</code> will be drawn to the left by the value of the <code>desiredThreshold</code> constant (in this case 10 pixels).</p>
  
  <p>So far so good; everything works well and the drawing is done correctly. However...</p>
  
  <p>The problem is that although the item is drawn slightly to the left(which is ok), the mouse events are still working as if the item didn't move at all; here's how it works:</p>
  
  <p><a href="https://i.stack.imgur.com/y1VFF.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/y1VFF.gif" alt="enter image description here"></a></p>
  
  <p>Is it possible to tell the delegate that the whole item is drawn slightly to the left so that when I click on the checkbox(which was also moved together with the whole item) the model would be correctly updated?</p>