Create a ComboBox widget in Gtk2HS

≯℡__Kan透↙ 提交于 2019-12-11 04:43:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!