質問

Haskellを介して複数の列を備えたListStoreモデルを使用して、GTKに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

しかし、すべての場合において、列ヘッダーと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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top