Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QStyledItemDelegate + QTreeView::setIndenation causes glitches on text drawing



  • It has been two days I can not get the solution.

    I have a custom QStyledItemDelegate and a QTreeView. QTreeView's indentation set to 12. And QStyledItemDelegate's QStyleOptionViewItem::rect set to -12. This set up causes glitches when I change the color of the QTreeView item's text.

    0_1552579096612_ыы.gif

    void MyItemViewDelegate::initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index) const
    {
        if (m_functor->operator ()(index))
        {
            option->rect.adjust(-12, 0, 0, 0);
        }    
    }
    
    MyTreeView::MyTreeView(QWidget* parent)
        : QTreeView(parent)
    {
        setIndentation(12);
    }
    

    What did I miss?



  • So What I forgot to do was to override:

    QRect QTreeView::visualRect(const QModelIndex& index) const
       {
           if (m_functor->operator ()(index))
           {
               return QTreeView::visualRect(index).adjusted(-m_indentation, 0, 0, 0);
           }
    
          return QTreeView::visualRect(index);
       }
    

Log in to reply