views:

55

answers:

1

Hello,

Im using tutorials from wxHaskell and want to display content of table movies in the grid. HEre is my code :

{--------------------------------------------------------------------------------
   Test Grid.
--------------------------------------------------------------------------------}

module Main where

import Graphics.UI.WX
import Graphics.UI.WXCore hiding (Event)

import Database.HDBC.Sqlite3 (connectSqlite3)
import Database.HDBC


main  
  = start gui

gui :: IO ()
gui 
  = do f <- frame [text := "Grid test", visible := False] 

       -- grids
       g <- gridCtrl f []
       gridSetGridLineColour g (colorSystem Color3DFace)
       gridSetCellHighlightColour g black
       appendColumns g (movies) -- Here is error: 

--        Couldn't match expected type `[String]'
--       against inferred type `IO [[String]]'


       --appendRows g (map show [1..length (tail movies)])
       --mapM_ (setRow g) (zip [0..] (tail movies))
       gridAutoSize g

       -- layout
       set f [layout := column 5 [fill (dynamic (widget g))]
             ]       
       focusOn g
       set f [visible := True]  -- reduce flicker at startup.
       return ()
    where

    movies = do 
        conn <- connectSqlite3 "Spop.db"
        r <- quickQuery' conn "SELECT id, title, year, description from Movie where id = 1" []
        let myResult = map convRow r
        return myResult

setRow g (row,values)
  = mapM_ (\(col,value) -> gridSetCellValue g row col value) (zip [0..] values)



{--------------------------------------------------------------------------------
   Library?f
--------------------------------------------------------------------------------}

gridCtrl :: Window a -> [Prop (Grid ())] -> IO (Grid ())
gridCtrl parent props
  = feed2 props 0 $
    initialWindow $ \id rect -> \props flags ->
    do g <- gridCreate parent id rect flags
       gridCreateGrid g 0 0 0
       set g props
       return g

appendColumns :: Grid a -> [String] -> IO ()
appendColumns g []
  = return ()
appendColumns g labels
  = do n <- gridGetNumberCols g
       gridAppendCols g (length labels) True
       mapM_ (\(i,label) -> gridSetColLabelValue g i label) (zip [n..] labels)

appendRows :: Grid a -> [String] -> IO ()
appendRows g []
  = return ()
appendRows g labels
  = do n <- gridGetNumberRows g
       gridAppendRows g (length labels) True
       mapM_ (\(i,label) -> gridSetRowLabelValue g i label) (zip [n..] labels)

convRow :: [SqlValue] -> [String]
convRow [sqlId, sqlTitle, sqlYear, sqlDescription] = [intid, title, year, description]
              where intid = (fromSql sqlId)::String
                    title = (fromSql sqlTitle)::String
                    year = (fromSql sqlYear)::String
                    description = (fromSql sqlDescription)::String

What should I do to get rif of error in code above (24th line)

+3  A: 

The error message says that appendColumns g expects a value of type [String], but that movies is of type IO [[String]].

So you need to fix two things:

  1. movies is an IO-action that returns a value, but you need the value it returns.

    Replace

    appendColumns g (movies)
    

    with

    movieList <- movies
    appendColumns g movieList
    

    (Incidentally, the brackets in the first line? They don't do anything.)

  2. You need to feed appendColumns g a list of strings, but you are trying to give it a list of lists of strings. You either need to turn the list of lists into a list of strings, or you need to give each list of strings to appendColumns g in turn.

    I'm guessing you want the latter, so you get a row on-screen for each row in the database. (I'm not familiar with wxhaskell, so I may misunderstand what appendColumns does.)

    movieList <- movies
    mapM_ (appendColumns g) movieList
    
Dave Hinton
Thanks, now it works great but I have a question:how do You know that movies is an IO action ?why if movies is IO action then x <- movies x - isnt The same type as movies ?what is the difference between = and <- I dont understand that :/
gruber
Read http://blog.sigfpe.com/2006/08/you-could-have-invented-monads-and.html
Dave Hinton