模拟鼠标悬停在QTreeView的项目上

时间:2022-01-23 08:29:16

In Qt package, is it possible to achieve highlighting (as seen when hovering with the mouse) of an item in QTreeView programmatically?

在Qt包中,是否可以通过编程方式实现QTreeView中项目的突出显示(如用鼠标悬停时所见)?

I am able to select the item and it gives a similar impression, but I cannot use this method (since I am using selections in some other place).

我能够选择项目,它给出了类似的印象,但我不能使用这种方法(因为我在其他地方使用选择)。

The last option would be to store a flag in the item itself and return a background or a font from data() method for some role. But this approach seems very tedious.

最后一个选项是在项目本身中存储一个标志,并从某个角色的data()方法返回背景或字体。但这种做法似乎非常繁琐。

模拟鼠标悬停在QTreeView的项目上

1 个解决方案

#1


0  

I ended up following vahancho advice.

我最后听了vahancho的建议。

In my model I have added the following in the data(QModelIndex index) method:

在我的模型中,我在数据(QModelIndex索引)方法中添加了以下内容:

if (role == Qt::BackgroundColorRole)
{
    if (HasHighlighted && (index == LastHighlightedIndex))
    {
        return qVariantFromValue(QColor(229, 243, 255)); //highlighting color, at least on Windows10
    }
}

To highlight I call

突出我打电话

void TreeModel::SetHighlighted(QModelIndex & index)
{
    if (index != LastHighlightedIndex || !HasHighlighted)
    {
        if (HasHighlighted)
        {
            emit dataChanged(LastHighlightedIndex, LastHighlightedIndex); //clearing previous highlighted 
        }
        LastHighlightedIndex = index;
        HasHighlighted = true;
        emit dataChanged(index, index);
    }

}

In my case I use a proxy so I need to get the right index: with the GetModelIndex method

在我的情况下,我使用代理,所以我需要获得正确的索引:使用GetModelIndex方法

void CustomTreeView::SimulateHover(QModelIndex index)
{
    TreeProxyModel *proxy = dynamic_cast<TreeProxyModel*>(this->model());
    if (proxy != nullptr)
    {
        proxy->model()->SetHighlighted(proxy->GetModelIndex(index));
    }
}

-

QModelIndex TreeProxyModel::GetModelIndex(QModelIndex & index)
    {
        return mapToSource(index);
    }

In addition to clear last hover:

除了明确的最后悬停:

void CustomTreeView::ClearSimulatedHover()
{
    TreeProxyModel *proxy = dynamic_cast<TreeProxyModel*>(this->model());
    if (proxy != nullptr)
    {
        proxy->model()->ClearHighlighted();
    }
}

#1


0  

I ended up following vahancho advice.

我最后听了vahancho的建议。

In my model I have added the following in the data(QModelIndex index) method:

在我的模型中,我在数据(QModelIndex索引)方法中添加了以下内容:

if (role == Qt::BackgroundColorRole)
{
    if (HasHighlighted && (index == LastHighlightedIndex))
    {
        return qVariantFromValue(QColor(229, 243, 255)); //highlighting color, at least on Windows10
    }
}

To highlight I call

突出我打电话

void TreeModel::SetHighlighted(QModelIndex & index)
{
    if (index != LastHighlightedIndex || !HasHighlighted)
    {
        if (HasHighlighted)
        {
            emit dataChanged(LastHighlightedIndex, LastHighlightedIndex); //clearing previous highlighted 
        }
        LastHighlightedIndex = index;
        HasHighlighted = true;
        emit dataChanged(index, index);
    }

}

In my case I use a proxy so I need to get the right index: with the GetModelIndex method

在我的情况下,我使用代理,所以我需要获得正确的索引:使用GetModelIndex方法

void CustomTreeView::SimulateHover(QModelIndex index)
{
    TreeProxyModel *proxy = dynamic_cast<TreeProxyModel*>(this->model());
    if (proxy != nullptr)
    {
        proxy->model()->SetHighlighted(proxy->GetModelIndex(index));
    }
}

-

QModelIndex TreeProxyModel::GetModelIndex(QModelIndex & index)
    {
        return mapToSource(index);
    }

In addition to clear last hover:

除了明确的最后悬停:

void CustomTreeView::ClearSimulatedHover()
{
    TreeProxyModel *proxy = dynamic_cast<TreeProxyModel*>(this->model());
    if (proxy != nullptr)
    {
        proxy->model()->ClearHighlighted();
    }
}