2011-03-19 4 views
0

Haskell을 통해 여러 열이있는 ListStore 모델을 사용하여 GTK에서 TreeView의 데이터를 강제로 표시 할 수 없습니다. 다음 코드가 있습니다Gtk2hs 다중 열 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

나는 중요한 것을 놓쳤다. ListStore 모델은 다형성을 갖기 때문에 모델이 주어진 객체에서 데이터를 추출해야하는 방법을 설명해야합니다. 때마다 당신이 (텍스트 렌더러 예)와 같은 코드를 작성해야 새 열을 추가 : row가 데이터입니다

cellLayoutSetAttributes yourColumn yourRenderer model (\row -> [ cellText := yourPowerfulValueExtractionFunction 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