Gtk2hs multiple column TreeView with ListStore issue
I cannot force GTK to render data in TreeView with ListStore model with multiple columns through Haskell. I have the following code
addTextColumn view name =
do
col <- treeViewColumnNew
rend <- cellRendererTextNew
treeViewColumnSetTitle col name
treeViewColumnPackStart col rend True
treeViewColumnSetExpand col True
treeViewAppendColumn view col
prepareTreeV开发者_Python百科iew view =
do
addTextColumn view "column1"
addTextColumn view "column2"
--adding data here
Then I try to add some data, and there are problems. I tried these:
--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
But in all cases I see the table with column header and one blank row inserted. Any help will be appreciated.
I have been missed an important thing. Since the ListStore model is polymorphic you must describe HOW the model should extract data from objects given to it. Every time you add the new column you should write the code like this (text renderer example):
cellLayoutSetAttributes yourColumn yourRenderer model (\row -> [ cellText := yourPowerfulValueExtractionFunction row ])
where row
is your data.
So this is the code of the solution with data implemented with "variant 1" (see question):
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
精彩评论