Gtk2hs多列TreeView与ListStore问题

时间:2011-03-19 17:18:55

标签: haskell gtk2hs

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

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

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

1 个答案:

答案 0 :(得分: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