0

我不能强制 GTK 通过 Haskell 使用具有多列的 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

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

4

1 回答 1

4

我错过了一件重要的事情。由于 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
于 2011-03-20T06:36:38.473 回答