Gtk2hs 多列 TreeView 与 ListStore 问题

发布于 2024-10-24 08:20:07 字数 1059 浏览 3 评论 0原文

我无法通过 Haskell 强制 GTK 使用具有多列的 ListStore 模型在 TreeView 中呈现数据。我有以下代码

addTextColumn view name =
    do
    col <- treeViewColumnNew
    rend <- cellRendererTextNew
    treeViewColumnSetTitle col name
    treeViewColumnPackStart col rend True
    treeViewColumnSetExpand col True
    treeViewAppendColumn view col

prepareTreeView view = 
    do
    addTextColumn view "column1"
    addTextColumn view "column2"

    --adding data here

然后我尝试添加一些数据,但出现问题。我尝试了这些:

    --variant 1 (data TRow = TRow {one::String, two::String}
    model <- listStoreNew ([] :: [TRow])
    listStoreAppend model $ TRow { one = "Foo", two = "Boo" }
    treeViewSetModel view model

    --variant 2
    model <- listStoreNew ([] :: [[String]])
    listStoreAppend model ["foo","boo"]
    treeViewSetModel view model

    --variant 3
    model <- listStoreNew ([] :: [(String, String)])
    listStoreAppend model ("foo", "boo")
    treeViewSetModel view model

但在所有情况下,我都会看到带有列标题和插入一个空白行的表格。任何帮助将不胜感激。

I cannot force GTK to render data in TreeView with ListStore model with multiple columns through Haskell. I have the following code

addTextColumn view name =
    do
    col <- treeViewColumnNew
    rend <- cellRendererTextNew
    treeViewColumnSetTitle col name
    treeViewColumnPackStart col rend True
    treeViewColumnSetExpand col True
    treeViewAppendColumn view col

prepareTreeView view = 
    do
    addTextColumn view "column1"
    addTextColumn view "column2"

    --adding data here

Then I try to add some data, and there are problems. I tried these:

    --variant 1 (data TRow = TRow {one::String, two::String}
    model <- listStoreNew ([] :: [TRow])
    listStoreAppend model $ TRow { one = "Foo", two = "Boo" }
    treeViewSetModel view model

    --variant 2
    model <- listStoreNew ([] :: [[String]])
    listStoreAppend model ["foo","boo"]
    treeViewSetModel view model

    --variant 3
    model <- listStoreNew ([] :: [(String, String)])
    listStoreAppend model ("foo", "boo")
    treeViewSetModel view model

But in all cases I see the table with column header and one blank row inserted. Any help will be appreciated.

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

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

发布评论

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

评论(1

原来分手还会想你 2024-10-31 08:20:07

我错过了一件重要的事情。
由于 ListStore 模型是多态的,因此您必须描述模型应如何从给定的对象中提取数据。每次添加新列时,您都应该编写如下代码(文本渲染器示例):

cellLayoutSetAttributes yourColumn yourRenderer model (\row -> [ cellText := yourPowerfulValueExtractionFunction row ])

其中 row 是您的数据。

这是使用“变体 1”实现的数据的解决方案的代码(参见问题):

prepareTreeView view = 
    do
    model <- listStoreNew ([] :: [TRow])

    addTextColumn view model one "one"
    addTextColumn view model two "two"

    listStoreAppend model $ TRow { one = "foo", two = "boo" }

    treeViewSetModel view model

-- 
addTextColumn view model f name =
    do
    col <- treeViewColumnNew
    rend <- cellRendererTextNew
    treeViewColumnSetTitle col name
    treeViewColumnPackStart col rend True
    -- LOOK HERE:
    cellLayoutSetAttributes col rend model (\row -> [ cellText := f row ])

    treeViewColumnSetExpand col True
    treeViewAppendColumn view col

I have been missed an important thing.
Since the ListStore model is polymorphic you must describe HOW the model should extract data from objects given to it. Every time you add the new column you should write the code like this (text renderer example):

cellLayoutSetAttributes yourColumn yourRenderer model (\row -> [ cellText := yourPowerfulValueExtractionFunction row ])

where row is your data.

So this is the code of the solution with data implemented with "variant 1" (see question):

prepareTreeView view = 
    do
    model <- listStoreNew ([] :: [TRow])

    addTextColumn view model one "one"
    addTextColumn view model two "two"

    listStoreAppend model $ TRow { one = "foo", two = "boo" }

    treeViewSetModel view model

-- 
addTextColumn view model f name =
    do
    col <- treeViewColumnNew
    rend <- cellRendererTextNew
    treeViewColumnSetTitle col name
    treeViewColumnPackStart col rend True
    -- LOOK HERE:
    cellLayoutSetAttributes col rend model (\row -> [ cellText := f row ])

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