【问题标题】:Gtk2hs multiple column TreeView with ListStore issueGtk2hs 多列 TreeView 与 ListStore 问题
【发布时间】:2011-03-19 17:18:54
【问题描述】:

我不能强制 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

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

【问题讨论】:

    标签: haskell gtk2hs


    【解决方案1】:

    我错过了一件重要的事情。 由于 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-19
      • 1970-01-01
      • 2015-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多