2011-03-19 21 views
0

我無法強制GTK通過Haskell通過帶有多列的ListStore模型在TreeView中呈現數據。我有以下代碼Gtk2hs多列TreeView與ListStore問題

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

我錯過了一件重要的事情。 由於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