質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Haskell

Haskellは高い機能性をもった関数型プログラミング言語で、他の手続き型プログラミング言語では難しいとされている関数でも容易に行うことができます。強い静的型付け、遅延評価などに対応しています。

Q&A

解決済

2回答

1584閲覧

TextViewから値を取り出したい

apo

総合スコア349

Haskell

Haskellは高い機能性をもった関数型プログラミング言語で、他の手続き型プログラミング言語では難しいとされている関数でも容易に行うことができます。強い静的型付け、遅延評価などに対応しています。

0グッド

0クリップ

投稿2020/11/11 08:57

編集2020/11/11 09:03

前提・実現したいこと

HaskellでGUIプログラミングを勉強しています。
haskell-giライブラリを使用しています。
公式サイトのHelloWorldプログラムにある、ラベルとボタンを配置したプログラムに修正を加え、テキストボックス(TextView)を追加することができました。

ボタンを押すとテキストボックスの値をラベルに書き換えるようにしたいのですが、テキストボックスから値を取り出すことができません。

公式サイトを見ていると「textViewGetBuffer」というメソッドで取り出せそうですが、使い方(書き方)がわかりません。

発生している問題・エラーメッセージ

想像で書いてみますが、当然のごとくエラーとなります。

[apo@myhostname my-project]$ stack build my-project> configure (lib + exe) Configuring my-project-0.1.0.0... my-project> build (lib + exe) Preprocessing library for my-project-0.1.0.0.. Building library for my-project-0.1.0.0.. [3 of 3] Compiling Hello /home/apo/doc/project/make/haskell/ClickMe/my-project/src/Hello.hs:26:39: error: ~ Couldn't match expected type ‘[AttrOp (m0 Gtk.TextBuffer) 'Data.GI.Base.Attributes.AttrConstruct]’ with actual type ‘Gtk.TextView’ ~ In the second argument of ‘new’, namely ‘textview’ In a stmt of a 'do' block: buffer <- new Gtk.textViewGetBuffer textview In the expression: do Gtk.init Nothing win <- new Gtk.Window [#title := "Introduction"] on win #destroy Gtk.mainQuit #resize win 640 480 .... | 26 | buffer <- new Gtk.textViewGetBuffer textview | ^^^^^^^^ /home/apo/doc/project/make/haskell/ClickMe/my-project/src/Hello.hs:44:29: error: ~ Couldn't match type ‘Data.Text.Internal.Text’ with ‘[Char]’ arising from a use of ‘:=’ ~ In the expression: #label := (show buffer) In the second argument of ‘set’, namely ‘[#label := (show buffer)]’ In the third argument of ‘on’, namely ‘(set msg [#label := (show buffer)])’ | 44 | on btn #clicked (set msg [#label := (show buffer)]) | ^^^^^^^^^^^^^^^^^^^^^^^ -- While building package my-project-0.1.0.0 (scroll up to its section to see the error) using: /home/apo/.stack/setup-exe-cache/x86_64-linux-tinfo6/Cabal-simple_mPHDZzAJ_3.0.1.0_ghc-8.8.4 --builddir=.stack-work/dist/x86_64-linux-tinfo6/Cabal-3.0.1.0 build lib:my-project exe:my-project-exe --ghc-options " -fdiagnostics-color=always" Process exited with code: ExitFailure 1

該当のソースコード

haskell

1{-# LANGUAGE OverloadedStrings, OverloadedLabels #-} 2 3module Hello (hello) where 4 5import qualified GI.Gtk as Gtk 6import Data.GI.Base 7-- import Graphics.UI.Gtk 8 9hello :: IO () 10hello = do 11 Gtk.init Nothing 12 13 win <- new Gtk.Window [ #title := "Introduction" ] 14 on win #destroy Gtk.mainQuit 15 #resize win 640 480 16 17 box <- new Gtk.Box [ #orientation := Gtk.OrientationVertical] 18 #add win box 19 20 msg <- new Gtk.Label [#label := "Hello"] 21 #add box msg 22 23 textview <- new Gtk.TextView [] 24 #add box textview 25 26 buffer <- new Gtk.textViewGetBuffer textview 27 28 btn <- new Gtk.Button [#label := "Click me!"] 29 #add box btn 30 on btn #clicked (set msg [#label := (show buffer)]) 31 32 #showAll win 33 34 Gtk.main

試したこと

「haskell-gi TextView textViewGetBuffer」などで検索してみましたが、参考になるサイトは見つかりませんでした。

補足情報(FW/ツールのバージョンなど)

OS:ホスト Windows10
ゲスト ArchLinux(VirtualBox)
stack: Version 2.5.1
LTS: lts-16.21

全コード
https://github.com/ringo-apo/haskell-gi-textview

ご教示よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

※申し訳なくも手元に環境を用意できてないので試せてはいません。あしからず。

textViewGetBuffer で取り出した TextBuffer から textBufferGetText (ドキュメントはこちら)という関数でText型が取り出せるみたいですが、これではダメですか?

他の言語のGTK+のバインディングを使ったサンプルを見ても、該当するメソッドを使っているようです。

投稿2020/11/11 09:34

編集2020/11/11 09:37
igrep

総合スコア428

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

apo

2020/11/11 13:18

回答ありがとうございます。試してみます。
guest

0

ベストアンサー

igrepさんの回答に加えて、エラーメッセージから読み解ける内容ですが

buffer <- new Gtk.textViewGetBuffer textview

buffer <- Gtk.textViewGetBuffer textview でよさそうです(newが要らない)

on btn #clicked (set msg [#label := (show buffer)])

textBufferGetText で取得した値を入れるなら、showは不要です。

投稿2020/11/11 15:07

as_capabl

総合スコア9

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

apo

2020/11/11 15:47

回答ありがとうございます。 buffer <- Gtk.textViewGetBuffer textview で取り出しのエラーは消えました。 書き出しの方を on btn #clicked (set msg [#label := (buffer)]) にすると下記エラーになりました。 ``` /home/apo/doc/project/make/haskell/haskell-gi-textview/my-project/src/Hello.hs:44:29: error: ~ Couldn't match type ‘Data.Text.Internal.Text’ with ‘Gtk.TextBuffer’ arising from a use of ‘:=’ ~ In the expression: #label := (buffer) In the second argument of ‘set’, namely ‘[#label := (buffer)]’ In the third argument of ‘on’, namely ‘(set msg [#label := (buffer)])’ | 44 | on btn #clicked (set msg [#label := (buffer)]) | ```
apo

2020/11/12 17:47

ご教示ありがとうございます。調べてみます。
apo

2021/05/27 00:34

TextViewは複数行入力のテキストボックスのようなので、単行入力のEntryで作り直しました。上記アドバイスを基にコーディングしたら動きました。ありがとうございます。https://github.com/ringo-apo/haskell-gi_entry
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問