Qyoto (Qt) を用いたリストウィジェットとコンボボックスを紹介します。
まず、Fedora 20 における Mono / Qyoto の開発環境の主要なパッケージですが、前回と同じく以下の通りです。
- mono-core-3.4.0-2.fc20.x86_64
- qt-4.8.6-5.fc20.x86_64
- qyoto-4.12.5-1.fc20.x86_64
- qyoto-devel-4.12.5-1.fc20.x86_64
リストウィジェット
リストウィジェット QListWidget はあらかじめ用意された選択肢の中から 1 つあるいは複数の項目を選択するためのウィジェットです。
using System;
using System.Collections.Generic;
using Qyoto;
/**
* ZetCode Qyoto C# tutorial
*
* This program uses the QListWidget widget.
* The option selected from the lw box is
* displayed in the label widget.
*
* @author Jan Bodnar
* website zetcode.com
* last modified October 2012
*
* modified by Fujito Suguri
* last modified 27-May-2014
*/
public class QyotoApp : QWidget
{
public QyotoApp ()
{
WindowTitle = "リストウィジェット";
InitUI ();
Move (100, 100);
Show ();
}
public void InitUI ()
{
QLabel label = new QLabel ("都道府県リスト");
QListWidget lw = new QListWidget ();
label.SetSizePolicy (QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed);
lw.SetSizePolicy (QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Preferred);
List prefList = new List (new string[] {
"北海道", "青森県", "岩手県", "宮城県", "秋田県", "山形県", "福島県", "茨城県", "栃木県",
"群馬県", "埼玉県", "千葉県", "東京都", "神奈川県", "山梨県", "長野県", "新潟県", "富山県",
"石川県", "福井県", "岐阜県", "静岡県", "愛知県", "三重県", "滋賀県", "京都府", "大阪府",
"兵庫県", "奈良県", "和歌山県", "鳥取県", "島根県", "岡山県", "広島県", "山口県", "徳島県",
"香川県", "愛媛県", "高知県", "福岡県", "佐賀県", "長崎県", "熊本県", "大分県", "宮崎県",
"鹿児島県", "沖縄県"
});
foreach (string pref in prefList) {
lw.AddItem (pref);
}
Connect (lw, SIGNAL ("itemSelectionChanged()"), this, SLOT ("OnSelected()"));
QVBoxLayout vbox = new QVBoxLayout (this);
vbox.AddWidget (label);
vbox.AddWidget (lw);
}
[Q_SLOT]
public void OnSelected ()
{
QListWidget l = (QListWidget)Sender ();
Console.WriteLine (l.CurrentItem ().Text () + "が選択されました。");
}
[STAThread]
public static int Main (String[] args)
{
new QApplication (args);
new QyotoApp ();
return QApplication.Exec ();
}
}
コンパイルおよび実行例を以下に示します。
$ mcs listwidget.cs -pkg:qyoto $ mono listwidget.exe 岩手県が選択されました。 福島県が選択されました。
コンボボックス
コンボボックス QComboBox は文字入力あるいは表示のための矩形領域と項目選択リストを組み合わせたウィジェットです。
using System;
using System.Collections.Generic;
using Qyoto;
/**
* ZetCode Qyoto C# tutorial
*
* This program uses the QComboBox widget.
* The option selected from the combo box is
* displayed in the label widget.
*
* @author Jan Bodnar
* website zetcode.com
* last modified October 2012
*
* modified by Fujito Suguri
* last modified 27-May-2014
*/
public class QyotoApp : QWidget
{
public QyotoApp ()
{
WindowTitle = "コンボボックス";
InitUI ();
Move (100, 100);
Show ();
}
public void InitUI ()
{
QLabel label = new QLabel ("都道府県リスト");
QComboBox combo = new QComboBox (this);
label.SetSizePolicy (QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Preferred);
combo.SetSizePolicy (QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Preferred);
List prefList = new List (new string[] {
"北海道", "青森県", "岩手県", "宮城県", "秋田県", "山形県", "福島県", "茨城県", "栃木県",
"群馬県", "埼玉県", "千葉県", "東京都", "神奈川県", "山梨県", "長野県", "新潟県", "富山県",
"石川県", "福井県", "岐阜県", "静岡県", "愛知県", "三重県", "滋賀県", "京都府", "大阪府",
"兵庫県", "奈良県", "和歌山県", "鳥取県", "島根県", "岡山県", "広島県", "山口県", "徳島県",
"香川県", "愛媛県", "高知県", "福岡県", "佐賀県", "長崎県", "熊本県", "大分県", "宮崎県",
"鹿児島県", "沖縄県"
});
foreach (string pref in prefList) {
combo.AddItem (pref);
}
Connect (combo, SIGNAL ("activated(QString)"), this, SLOT ("OnActivated(QString)"));
QHBoxLayout hbox = new QHBoxLayout (this);
hbox.AddWidget (label);
hbox.AddWidget (combo);
}
[Q_SLOT]
public void OnActivated (string text)
{
Console.WriteLine (text + "が選択されました。");
}
[STAThread]
public static int Main (String[] args)
{
new QApplication (args);
new QyotoApp ();
return QApplication.Exec ();
}
}
コンパイルおよび実行例を以下に示します。
$ mcs combobox.cs -pkg:qyoto $ mono combo.exe 岩手県が選択されました。 福島県が選択されました。
参考サイト
- C# Qyoto tutorial
- Qyoto: Main Page - Qyoto Documentation
























