RingQt アプリケーション用のオブジェクトライブラリ¶
RingQt アプリケーション用のオブジェクトライブラリを学びます。
Ring には RingQt アプリケーション用のオブジェクトライブラリが付属しています。 ウィンドウオブジェクトへのグローバル変数の使用、 およびオブジェクト名を使用してイベントをオブジェクトへ接続するのではなく、 オブジェクトライブラリは GUI オブジェクトの管理を行います。また、同一クラスから一枚以上のウィンドウを手軽に作成するために、より自然な API を提供しています。 さらにライブラリでは、イベントが発行されたときに実行されるメソッドを手軽に設定する方法を提供しています。 同じく子またはサブウィンドウから親または呼び出し元のウィンドウを手軽に使うための自然なインタフェースを提供しています。
オブジェクトライブラリは MVC デザインパターンを使用して設計されています。
オブジェクトライブラリは RingQt へ統合されており RingQt から直接使用することができます。
ライブラリの用法¶
- Open_Window(cWindowControllerClassName) 関数で新しいウィンドウを開きます。
- 最低でも Controller と View のクラス二本を各ウィンドウごとに作成してください。
- WindowsControllerParent クラスから各 Controller クラスを作成します。
- WindowsViewParent クラスから各 View クラスを作成します。
- Last_Window() 関数を使用すると最後に作成されたウィンドウのオブジェクト (Controller オブジェクト) を取得します。
- サブウィンドウを呼び出すときは SetParentObject() メソッドの使用、および Self オブジェクトを渡します。
- View クラスでは、イベントメソッドを決定するために Method(cMethodName) 関数を使用します。
- Method(cMethodName) 関数を実行することで Controller クラスでメソッドを決定します。
- 各 Controller クラスはデフォルトで CloseAction() クラスがあり、呼び出すことでウィンドウを閉じます。
- 各ウィンドウに対して Show() メソッドの呼び出しは不要です。 Open_Window() の使用時に呼び出します。
- View クラスでは GUI ウィンドウオブジェクトを win 属性で定義します。
- Open_WindowNoShow() 関数を使用するとウィンドウが表示されるのを回避できます。
- Open_WindowAndLink() を使用するとメソッドからウィンドウへ手軽にアクセスできます。
用例¶
この用例では、二種類のウィンドウを作成します。
- メインウィンドウにはボタンがあります。ユーザがボタンをクリックしたときにサブウィンドウが開かれます。
- ユーザは複数のサブウィンドウを開くためにボタンを何回もクリックできます。
- 各サブウィンドウには二つのボタンがあります。
- 最初のボタンはサブウィンドウにありメイン、およびサブウィンドウのタイトルのタイトルを変更します。
- 次のボタンはサブウィンドウにありサブウィンドウを閉じます。
load "guilib.ring"
new qApp {
open_window( :MainWindowController )
exec()
}
class MainWindowController from WindowsControllerParent
oView = new MainWindowView
func SubWindowAction
Open_window( :SubWindowController )
Last_Window().SetParentObject(self)
class MainWindowView from WindowsViewParent
win = new qWidget() {
SetWindowTitle("Main Window")
btnSub = new qPushButton(win) {
setText("Sub Window")
setClickEvent( Method( :SubWindowAction ) )
}
resize(400,400)
}
class SubWindowController from WindowsControllerParent
oView = new SubWindowView
func SetMainWindowTitleAction
Parent().oView.win.SetWindowTitle("Message from the Sub Window")
oView.win.SetWindowTitle("Click Event Done!")
class SubWindowView from WindowsViewParent
win = new qWidget() {
SetWindowTitle("Sub Window")
btnMsg = new qPushButton(win) {
setText("Set Main Window Title")
setClickEvent( Method( :SetMainWindowTitleAction ) )
}
btnClose = new qPushButton(win) {
Move(200,0)
setText("Close")
setClickEvent( Method( :CloseAction ) )
}
resize(400,400)
}
このスクリーンショットはサブウィンドウを三枚作成しています。

このスクリーンショットは各サブウィンドウでボタンをクリックしています。

Open_WindowAndLink() 関数¶
Open_WindowAndLink() 関数アプリケーションウィンドウの間で接続に使用でき、 オブジェクトの間でメッセージ (メッセージの呼び出し) を渡すことができます。
この関数は、ほかのウィンドウの作成で動的オブジェクトを使用するために、 メタプログラミングによる呼び出し元クラスで動的メソッドを定義するために使用します。
用例 : (フォームデザイナーを使用しています)
最初のウィンドウ
- https://github.com/ring-lang/ring/blob/master/applications/formdesigner/tests/twowindowspart5/firstwindowView.ring
- https://github.com/ring-lang/ring/blob/master/applications/formdesigner/tests/twowindowspart5/firstwindowController.ring
次のウィンドウ
- https://github.com/ring-lang/ring/blob/master/applications/formdesigner/tests/twowindowspart5/secondwindowView.ring
- https://github.com/ring-lang/ring/blob/master/applications/formdesigner/tests/twowindowspart5/secondwindowController.ring
このコードの用例では (FirstWindowController.ring より)
Open_WindowAndLink() は SecondWindowController クラスからオブジェクトを作成します。
そしてメソッドを追加します : FirstWindowController クラスへ SecondWindow(), IsSecondWindow() メソッドを追加
さらにメソッドを追加します : SecondWindowController クラスへ FirstWindow(), IsFirstWindow() メソッドを追加
よって FirstWindowController クラスにある SendMessage() メソッドは SecondWindow() メソッドでオブジェクトにアクセスするために使用できます。
これは Last_Window(), Parent() および SetParentObject() メソッドを使用するより、もっと簡単です。
class firstwindowController from windowsControllerParent
oView = new firstwindowView
func OpenSecondWindow
Open_WindowAndLink(:SecondWindowController,self)
func SendMessage
if IsSecondWindow()
SecondWindow().setMessage("Message from the first window")
ok
func setMessage cMessage
oView.Label1.setText(cMessage)
Open_WindowInPackages() 関数¶
Open_WindowInPackages() 関数は Open_Window() と同じですが、 ウィンドウを開く前にインポートを行うパッケージの追加リストを決定します。
文法:
Open_WindowInPackages(cClassName,aPackagesList)
用例:
この用例はフォームデザイナーのソースコードからの引用です。 open_windowInPackages() 関数でウィンドウフラグのウィンドウを開きます。
クラス名 “WindowFlagsController” とパッケージの名前を決定します。
ウィンドウフラグのウィンドウでは FormDesigner および System.GUI パッケージを使用しています。
open_windowInPackages(:WindowFlagsController,[
"formdesigner",
"System.GUI"
])
オブジェクトライブラリのソースコード¶
ライブラリのソースコードは非常に単純です。ソースコードファイルを確認できます。