indexwidget triggers a focusout event in a treeview

33 Views Asked by At

In a treeview, I add some indexwidgets to some items through delegate. The geometry of the indexwidget does not cover the full item rect, but when I click the area that outside the indexwidget which is a button but inside an item, that will trigger a focus event of the treeview.

This is my indexwidget inside an item:

enter image description here

Here's my overridden delegate paint function:

void CustomDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    QStyledItemDelegate::paint(painter, option, index);

    if (index.parent() == QModelIndex())
    {
        return;
    }

    if (m_view)
    {
        if (m_view->indexWidget(index))
        {
            return;
        }
        QWidget* edit = nullptr;
        QIcon iconVisible("");
        QIcon iconInvisible("");
        IndexWidgetButton* button = new IndexWidgetButton(iconVisible, iconInvisible);
        button->setFixedSize(16, 16);
        button->setFlat(true);
        button->setIconSize(QSize(16, 16));
        auto rect = option.rect;
        rect.adjust(16, 8, -32, -4);
        button->setGeometry(rect);
        edit = button;
        if (edit)
        {
            m_view->setIndexWidget(index, edit);
        }
    }
}

I tried to click the indexwidget which is a button, indexwidget responds, but it does not trigger a focus out event of the treeview.
When I click another area, it triggers it.

Why would that happen?

Environment:

  • IDE : Vs studio 2019
  • Qt : 5.15.2
0

There are 0 best solutions below