问题
I want to create a ComboBox widget with this code:
void initGUI
window <- windowNew
...
cb <- comboBoxNewText
comboBoxAppendText cb "Option 1"
comboBoxAppendText cb "Option 2"
comboBoxSetActive cb 0
boxPackStart hb cb PackNatural 0
...
But this error appears:
Couldn't match type ‘[Char]’
with ‘text-1.2.2.0:Data.Text.Internal.Text’
Expected type: ComboBoxText
Actual type: [Char]
In the second argument of ‘comboBoxAppendText’, namely
‘"Secuencial"’
In a stmt of a 'do' block: comboBoxAppendText cb "Secuencial"
In the expression:
do { void initGUI;
window <- windowNew;
set
window
[windowTitle := "A title",
windowDefaultWidth := 1024, ....];
vb <- vBoxNew False 7;
.... }
I am just following this tutorial (http://www.muitovar.com/gtk2hs/chap4-2.html) and reading the docs (http://projects.haskell.org/gtk2hs/docs/gtk2hs-docs-0.9.12/Graphics-UI-Gtk-ModelView-ComboBox.html#v%3AcomboBoxInsertText)
How can i make it work?
Thanks in advance.
回答1:
I recommend using the documentation on Hackage. The documentation you linked is probably a decade stale by now.
From that documentation, we have
type ComboBoxText = Text
comboBoxAppendText :: ComboBoxClass self => self -> ComboBoxText -> IO Int
You are passing "Option 1"
as the ComboBoxText
argument. In vanilla Haskell, this is a String
rather than a Text
-- as the error says. You can either pack
the String
, as in
import qualified Data.Text as T
comboBoxAppendText cb (T.pack "Option 1")
or turn on OverloadedStrings
to have this automatically done for String
literals, as in:
{-# LANGUAGE OverloadedStrings #-}
comboBoxAppendText cb "Option 1"
来源:https://stackoverflow.com/questions/45445882/create-a-combobox-widget-in-gtk2hs