在 Gtk 中,如何从按 x 和 y 坐标排序的 TreeView 中获取路径?

发布于 2024-08-20 20:31:59 字数 1407 浏览 10 评论 0原文

我有一个经过过滤然后排序的 ListStore。它看起来像这样:

// Create a model for the cards
cardListStore = new ListStore (typeof (Card));

// Set up the tree view
cardFilter = new TreeModelFilter (cardListStore, null);
cardFilter.VisibleFunc = new TreeModelFilterVisibleFunc (FilterCards);
cardSort = new TreeModelSort (cardFilter);
cardTreeView.Model = cardSort;

当我右键单击每一行时,我希望获得一个特定于每一行的上下文菜单。我的点击处理程序看起来像这样:

[GLib.ConnectBeforeAttribute]
void HandleCardTreeViewButtonPressEvent (object o, ButtonPressEventArgs args)
{
    if (args.Event.Button != 3)
        return;

    TreePath path;
    // If right click on empty space
    if (!cardTreeView.GetPathAtPos (Convert.ToInt32 (args.Event.X),
                                    Convert.ToInt32 (args.Event.Y),
                                    out path)) {
        MakeCardEmptySpaceContextMenu ().Popup ();
        return;
    }

    TreeIter iter;
    if (!cardListStore.GetIter (out iter, path))
        return;

    Card card = (Card) cardListStore.GetValue (iter, 0);

    MakeCardContextMenu (card, iter).Popup ();
}

当 ListStore 未过滤或排序时,这会起作用。但当它是时,它会给出错误的行。

例如,假设行在排序之前如下所示:

A
B
C

排序后,它们看起来像这样:

B
一个
C

右键单击​​第二行(“A”)将为您提供“B”,因为这是模型排序之前 B 所在的位置。过滤也会发生同样的情况。假设模型经过过滤后如下所示:

A
C

右键单击​​第二行(“C”)仍会给出“B”。

知道如何解决这个问题吗?

I have a ListStore that is filtered and then sorted. It looks something like this:

// Create a model for the cards
cardListStore = new ListStore (typeof (Card));

// Set up the tree view
cardFilter = new TreeModelFilter (cardListStore, null);
cardFilter.VisibleFunc = new TreeModelFilterVisibleFunc (FilterCards);
cardSort = new TreeModelSort (cardFilter);
cardTreeView.Model = cardSort;

I want to have a get a context menu specific for each row when I right-click on it. My click handler looks something like this:

[GLib.ConnectBeforeAttribute]
void HandleCardTreeViewButtonPressEvent (object o, ButtonPressEventArgs args)
{
    if (args.Event.Button != 3)
        return;

    TreePath path;
    // If right click on empty space
    if (!cardTreeView.GetPathAtPos (Convert.ToInt32 (args.Event.X),
                                    Convert.ToInt32 (args.Event.Y),
                                    out path)) {
        MakeCardEmptySpaceContextMenu ().Popup ();
        return;
    }

    TreeIter iter;
    if (!cardListStore.GetIter (out iter, path))
        return;

    Card card = (Card) cardListStore.GetValue (iter, 0);

    MakeCardContextMenu (card, iter).Popup ();
}

This works when the ListStore is not filtered or sorted. But when it is, it gives the wrong row.

For example, say the rows look like this before they are sorted:

A
B
C

And after they are sorted, they look like this:

B
A
C

Right-clicking on the second row ("A") will give you "B", because that's where B was before the model was sorted. The same thing happens for filtering. Say the model, after it is filtered, looks like this:

A
C

Right-clicking on the second row ("C") would still give you "B".

Any idea how to work around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

私藏温柔 2024-08-27 20:31:59

我只需要从cardSort 获取iter 和值,而不是cardListStore。

if (!cardListStore.GetIter (out iter, path))
    return;

Card card = (Card) cardListStore.GetValue (iter, 0);

变成

if (!cardSort.GetIter (out iter, path))
    return;

Card card = (Card) cardSort.GetValue (iter, 0);

I just needed to get the iter and the value from cardSort, instead of cardListStore.

if (!cardListStore.GetIter (out iter, path))
    return;

Card card = (Card) cardListStore.GetValue (iter, 0);

becomes

if (!cardSort.GetIter (out iter, path))
    return;

Card card = (Card) cardSort.GetValue (iter, 0);
恬淡成诗 2024-08-27 20:31:59

看起来你和数据之间有一个过滤器,这就是树显示的内容。你正在查看树后面的数据...

现在,我对 GTK 不熟悉,但也许类似

TreeModelSort.convert_path_to_child_path ?

There 必须有一种方法来获取你正在查看的 MODEL 的 VIEW 的哪一行,然后翻译它回到真实模型数据。

Looks like there is a filter between you and the data, and that is what is displayed by the tree. You are looking at the data behind the tree...

Now, I'm not familiar with GTK, but perhaps something like

TreeModelSort.convert_path_to_child_path ?

There must bet a way to get what row of the VIEW of the MODEL you are looking at, and then translate that back to real model data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文