【发布时间】:2016-03-07 15:28:28
【问题描述】:
我尝试使用haskell和Qt在ubuntu上使用sublime text 3创建一个单击按钮时显示文本的程序。 但显然在定义信号键时存在问题(该键将识别单击按钮时调用的信号)。 此外,很难找到有关 HsQML 的文档,即连接 haskell 和 Qt 的绑定。
代码:
module Main where
import Graphics.QML
import Control.Concurrent
import Control.Exception
import Data.IORef
import Data.Text (Text)
import qualified Data.Text as T
main :: IO ()
main = do
state <- newIORef $ T.pack ""
skey <- newSignalKey
clazz <- newClass [
defPropertySigRO' "my_label" skey (\_ -> readIORef state),
defMethod' "sayHello" (\obj txt -> do
writeIORef state txt
fireSignal skey obj
return ())]
ctx <- newObject clazz ()
runEngineLoop defaultEngineConfig {
initialDocument = fileDocument "exemple2.qml",
contextObject = Just $ anyObjRef ctx}
错误信息:
Build FAILED
/home/lowley/Documents/haskell/Qt/exemple-2.hs: line 13, column 10:
No instance for (SignalSuffix (IO a0))
arising from a use of `newSignalKey'
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there is a potential instance available:
instance SignalSuffix (IO ()) -- Defined in `Graphics.QML.Objects'
Possible fix:
add an instance declaration for (SignalSuffix (IO a0))
In a stmt of a 'do' block: skey <- newSignalKey
In the expression:
do { state <- newIORef $ T.pack "";
skey <- newSignalKey;
clazz <- newClass
[defPropertySigRO' "my_label" skey (\ _ -> readIORef state),
defMethod' "sayHello" (\ obj txt -> ...)];
ctx <- newObject clazz ();
.... }
In an equation for `main':
main
= do { state <- newIORef $ T.pack "";
skey <- newSignalKey;
clazz <- newClass
[defPropertySigRO' "my_label" skey (\ _ -> ...), ....];
.... }
已解决! 但是我想知道为什么这个程序可以编译没有上面的错误:
module Main where
import Graphics.QML
import Control.Concurrent
import Control.Exception
import Data.IORef
import qualified Data.Text as T
main :: IO ()
main = do
state <- newIORef $ T.pack ""
skey <- newSignalKey
clazz <- newClass [
defPropertySigRO' "result" skey (\_ ->
readIORef state),
defMethod' "factorial" (\obj txt -> do
let n = read $ T.unpack txt :: Integer
writeIORef state $ T.pack "Working..."
fireSignal skey obj
forkIO $ do
let out = T.take 1000 . T.pack . show $ product [1..n]
evaluate out
writeIORef state out
fireSignal skey obj
return ())]
ctx <- newObject clazz ()
runEngineLoop defaultEngineConfig {
initialDocument = fileDocument "factorial2.qml",
contextObject = Just $ anyObjRef ctx}
【问题讨论】:
-
并不是我提倡使用它,也不是你的问题的答案,但是haskell还有一个qt包。
标签: qt ubuntu haskell sublimetext3