How do I cast Widget to Label in Haskell's GI-Gtk?

本小妞迷上赌 提交于 2019-12-02 18:32:02

问题


I have this sample code where I have a ListBox containing ListBoxRows, which in turn contain a Label. When I click on the ListBox, I get a ListBoxRow. So far so good. The problems start when I want to interact with the ListBoxRows children.

I have used this function to get the Label, which is the child of the ListBoxRow. https://hackage.haskell.org/package/gi-gtk-3.0.18/docs/GI-Gtk-Objects-Container.html#g:13

However, the returned type is Widget. How do I convert the type of the object? Function widgetGetName tells me it is a label, but the Haskell Type system insists it is a Widget, so I can not use label specific functions.

working code

  _ <- onListBoxRowSelected listbox2 (\(Just r) -> do
                                         cc <- containerGetChildren r
                                         mlabel <- castTo Label (head cc)
                                         case mlabel of
                                           Nothing -> putStrLn "Not a label!"
                                           Just label -> (labelGetText label) >>= putStrLn . unpack)

Thanks to Dan


回答1:


Try this:

cc <- containerGetChildren r
mlabel <- castTo Label (head cc)
case mlabel of
  Nothing -> putStrLn “Not a label!”
  Just label -> labelGetText label >>= putStrLn


来源:https://stackoverflow.com/questions/47979509/how-do-i-cast-widget-to-label-in-haskells-gi-gtk

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