title: How to create a custom TreeView in qt with branches put at right side of the ItemView?
body: <p>What I want is to create a sidebar with qt with QTreeView that branches are put at right side of items. Like AdminLTE sidebar: <a href="https://adminlte.io/themes/AdminLTE/index2.html#" rel="nofollow noreferrer">https://adminlte.io/themes/AdminLTE/index2.html#</a>.</p>
  
  <p>I have Tried several solutions each failing my desired design:</p>
  
  <ol>
  <li>Using delegates</li>
  <li>Deriving TreeView class and reimplement DrawBranches</li>
  <li>Create a custom ProxyStyle</li>
  </ol>
  
  <p>The whole point is I am from a web development background and doing such things is super easy with it!</p>
  
  <p><strong><em>Edit:</em></strong></p>
  
  <p>Supposing we have a stupid ProxyStyle-derived class, what I should do for the situations below:</p>
  
  <pre><code>void CustomStyle::drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
  {
      if (element == PE_IndicatorBranch) {
          // put this damn thing at right
      }
      else if(element == PE_PanelItemViewItem) {
          // put this at left
      }
      // else
      QProxyStyle::drawPrimitive(element, option, painter, widget);
  }
  </code></pre>
  
  <p>or maybe using a delegate: </p>
  
  <pre><code>void TreeItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) const
  {
      // for example translate branch!? what to do????
      QStyledItemDelegate::paint(painter, option, index);
  
  }
  </code></pre>
  
  <p>or maybe reimplementing TreeView::DrawBranches.
  Either way, no idea what to do to fully accomplish my requirements.</p>
  
  <p><strong><em>Edit2:</em></strong></p>
  
  <p>This is it for now, but still not what I want, for example hovering makes off behavior:</p>
  
  <p>sidebar.cpp</p>
  
  <pre><code>#include "sidebar.h"
  
  #include &lt;QMouseEvent&gt;
  #include &lt;QPainter&gt;
  #include "sidebarstyle.h"
  #include "sidebardelegate.h"
  
  SideBar::SideBar(QWidget *parent)
      : QTreeView(parent)
  {
      setStyle(new SideBarStyle);
      setItemDelegate(new SideBarDelegate(this));
      setHeaderHidden(true);
      setEditTriggers(QAbstractItemView::NoEditTriggers);
      //    setIndentation(40);
      resize(230, parent-&gt;height());
      setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding));
  
      //    QPalette palette;
      //    palette.setBrush(QPalette::Base, QColor(34, 45, 50));
      //    palette.setBrush(QPalette::Highlight, Qt::transparent);
      //    setPalette(palette);
      setStyleSheet("QTreeView::item {height: 50px;} QTreeView {background-color: #222d32; color: #b8c7ce; font-size: 14px; font-weight: 400;} QTreeView::branch:selected, QTreeView::item:selected, QTreeView::branch:hover, QTreeView::item:hover {background-color: #1e282c; color: #fff}");
      //    setStyleSheet("QTreeView {background-color: #222d32; color: #b8c7ce}");
  
  
  }
  
  void SideBar::mousePressEvent(QMouseEvent *event)
  {
      QModelIndex index = indexAt(event-&gt;pos());
      bool lastState = isExpanded(index);
      QTreeView::mousePressEvent(event);
      setExpanded(index, !lastState);
  }
  
  void SideBar::drawBranches(QPainter *painter, const QRect &amp;rect, const QModelIndex &amp;index) const
  {
      QTreeView::drawBranches(painter, rect, index);
  }
  </code></pre>
  
  <p>sidebardelegate.cpp</p>
  
  <pre><code>#include "sidebardelegate.h"
  #include &lt;QPainter&gt;
  #include "state.h"
  
  SideBarDelegate::SideBarDelegate(QObject *parent)
      : QStyledItemDelegate(parent)
  {
      font.setFamily("FontAwesome");
      font.setPixelSize(28);
  
  }
  
  void SideBarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) const {
      QStyledItemDelegate::paint(painter, option, index);
      if(index.column() == 0){
          QRect _branch = QRect(option.widget-&gt;width() - option.decorationSize.width() -25, option.rect.y() + option.rect.height() / 4, option.decorationSize.width() + 15, option.rect.height() / 2);
          //        QRect icon = QRect(5, option.rect.y() + option.rect.height() / 4, option.decorationSize.width(), option.rect.height() / 2);
          QModelIndex _index = index;
          int depth = 0;
          while ( _index.parent().isValid() )
          {
              _index = _index.parent();
              depth++;
          }
          //        if(index.parent().isValid()) {
          QRect icon = QRect(5 + option.decorationSize.width() * depth, option.rect.y() + option.rect.height() / 4, option.decorationSize.width() + 10, option.rect.height() / 2);
          //        }
          painter-&gt;setFont(font);
          if(option.state &amp; QStyle::State_Children)
          {
              if(option.state &amp; QStyle::State_Open)
              {
                  //                painter-&gt;drawImage(_branch, QImage("/home/fisher/Pictures/Download.png"));
                  painter-&gt;drawText(_branch, "\uf107");
              }
              else
              {
                  //painter-&gt;drawImage(_branch, QImage("/home/fisher/Pictures/135210286.png"));
                  painter-&gt;drawText(_branch, "\uf104");
              }
              switch (index.row()) {
              case 0:
                  //                painter-&gt;drawImage(icon, QImage("/home/fisher/Pictures/135210286.png"));
                  painter-&gt;drawText(icon, "\uf080");
                  break;
              case 1:
                  //                painter-&gt;drawImage(icon, QImage("/home/fisher/Pictures/images.png"));
                  painter-&gt;drawText(icon, "\uf14a");
                  break;
              case 2:
                  //                painter-&gt;drawImage(icon, QImage("/home/fisher/Pictures/iamges.jpeg"));
                  painter-&gt;drawText(icon, "\uf0c3");
                  break;
              default:
                  //                painter-&gt;drawImage(icon, QImage("/home/fisher/Pictures/images (1).png"));
                  painter-&gt;drawText(icon, "\uf085");
              }
              if(option.state &amp; QStyle::State_Selected) {
                  QRect bar = QRect(0, option.rect.y(), 2, option.rect.height());
                  painter-&gt;setBrush(QColor(60, 141, 188));
                  painter-&gt;setPen(QColor(60, 141, 188));
                  painter-&gt;drawRect(bar);
              }
          } else {
                          adjustDefaultTextColors(option, *painter);
              painter-&gt;drawText(icon, "\uf111");
              //painter-&gt;drawImage(icon, QImage("/home/fisher/Pictures/images (1).jpeg"));
          }
      }
  }
  
  void SideBarDelegate::adjustDefaultTextColors(const QStyleOptionViewItem &amp;option, QPainter &amp;painter)  const
  {
      if(option.state &amp; QStyle::State_MouseOver) {
          painter.setPen(QColor(255, 255, 255));
      } else {
          painter.setPen(QColor(138, 164, 175));
      }
  }
  </code></pre>
  
  <p>sidebarstyle.cpp</p>
  
  <pre><code>#include "sidebarstyle.h"
  #include &lt;QStyleFactory&gt;
  #include &lt;QStyleOption&gt;
  #include &lt;QColor&gt;
  
  SideBarStyle::SideBarStyle()
      : QProxyStyle(QStyleFactory::create("linux"))
  {
  }
  
  void SideBarStyle::drawPrimitive(PrimitiveElement element,
                                   const QStyleOption *option,
                                   QPainter *painter,
                                   const QWidget *widget) const
  {
      if(element &amp; PE_IndicatorBranch) {
          return;
      }
  //    if(option-&gt;state &amp; QStyle::State_HasFocus){
  //        QStyleOptionViewItem myViewItemOption;
  //        const QStyleOptionViewItem *viewItemOption =
  //                qstyleoption_cast&lt;const QStyleOptionViewItem *&gt;(option);
  //        if (viewItemOption) {
  //            myViewItemOption = *viewItemOption;
  //            myViewItemOption.palette.setBrush(QPalette::Highlight, QColor(30, 40, 44));
  //            myViewItemOption.palette.setBrush(QPalette::HighlightedText, Qt::white);
  //        }
  //        QProxyStyle::drawPrimitive(element, &amp;myViewItemOption, painter, widget);
  //        return;
  //    }
      QProxyStyle::drawPrimitive(element, option, painter, widget);
  
  }
  
  //void SideBarStyle::drawControl(ControlElement element,
  //                               const QStyleOption *option,
  //                               QPainter *painter,
  //                               const QWidget *widget) const
  //{
  ////    if(option-&gt;state &amp; QStyle::State_HasFocus){
  ////        QStyleOptionViewItem myViewItemOption;
  ////        const QStyleOptionViewItem *viewItemOption =
  ////                qstyleoption_cast&lt;const QStyleOptionViewItem *&gt;(option);
  ////        if (viewItemOption) {
  ////            myViewItemOption = *viewItemOption;
  ////            myViewItemOption.palette.setBrush(QPalette::Highlight, QColor(30, 40, 44));
  ////            myViewItemOption.palette.setBrush(QPalette::HighlightedText, Qt::white);
  
  ////        }
  ////        QProxyStyle::drawControl(element, &amp;myViewItemOption, painter, widget);
  ////        return;
  ////    }
  //    QProxyStyle::drawControl(element, option, painter, widget);
  //}
  </code></pre>
  
  <p>current picture</p>
  
  <p><img src="https://i.stack.imgur.com/3WdE9.png" alt=""></p>
  
  <p>Also I borrowed some help from @eyllanesc answer to this question:
  <a href="https://stackoverflow.com/questions/55969916/how-to-remove-qtreeview-indentation">How to remove QTreeView indentation</a></p>