リナザウのソフトウェア開発


○ 第2章 ウィンドウを作成する「ダイアログを表示する」






さて、今回は、ダイアログを表示してみましょう。
前回作成したプログラムをさらに拡大してみます。




<CPPファイル名 main.cpp>

#include <qpe/qpeapplication.h>   // 使用するクラスを指定する

#include "mainwindow.h"   // 使用するプログラムを指定する

int main(int argc, char *argv[])
{
	QPEApplication a(argc,argv);
	Mainwindow *mw = new Mainwindow();
	a.showMainWidget(mw);
	return a.exec();
}


(解説は、かきかけ)



<ヘッダーファイル名 mainwindow.h>

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <qmainwindow.h>   // 使用するクラスを指定する
#include <qapplication.h>
#include <qpushbutton.h>
#include <qtextcodec.h>
#include <qfont.h>
#include <qmenubar.h>
#include <qdialog.h>
#include <qlabel.h>

#include "dialog.h"   // 使用するプログラムを指定する

class Mainwindow : public QWidget{
	Q_OBJECT

public:
	Mainwindow(QWidget *parent = 0, const char *name = 0);

private:
	QTextCodec *codec; // テキストコーデック
	QPushButton *button; // ボタン
	QMenuBar *menubar; // メニューバー
	QLabel *label; // ラベル
	
	Dialog *dlg;  //「dlg」を「dialog.h」で組んだダイアログ「Dialog」のために用意する

private slots:  // スロットを使うことで自分で機能を作れます
	void slot_button_text_change();  // slot_button_text_change() というスロットを作成する
	void slot_show_dialog();  // slot_show_dialog() というスロットを作成する
};

#endif //MAINWINDOW_H


(解説は、かきかけ)



<CPPファイル名 mainwindow.cpp>

#include "mainwindow.h"  // mainwindow.cpp のヘッダーファイル mainwindow.h を読み込み

Mainwindow::Mainwindow(QWidget *parent, const char *name)
		: QWidget(parent, name)
{
	// テキストコーデック「codec」を作成
	// 以降、codec->toUnicode("ここに書いたことは日本語で表示できる")
	codec = QTextCodec::codecForName("ShiftJIS");

	// ウィンドウのタイトルを設定
	setCaption(codec->toUnicode("サンプル3「ダイアログを作成する」"));

	// 使用するフォントを設定
	QFont f("lcfont",18);
	setFont(f);

	// ボタン「button」を作成(「Quit」と表示されたボタン)
	button = new QPushButton(codec->toUnicode("Quit"), this);
	// ボタン「button」の配置位置を指定 setGeometry(配置x座標,配置y座標,ボタンの横幅,ボタンの縦幅)
	button -> setGeometry(200, 80, 70, 25);
	
	// ラベル「label」を作成(「ダイアログ」と表示されたラベル)
	label = new QLabel(codec->toUnicode("ダイアログの状態"), this);
	// ラベル「label」の配置位置を指定 setGeometry(配置x座標,配置y座標,ボタンの横幅,ボタンの縦幅)
	label -> setGeometry(10, 150, 500, 30);
	
	// メニューバーを画面上に配置
	menubar = new QMenuBar( this );
	QPopupMenu *m_customize = new QPopupMenu(this);  // メニュー項目「m_customize」を作成
	m_customize->insertItem( codec->toUnicode("ボタンの文字をかえる"), this, SLOT(slot_button_text_change()) );  //項目「ボタンの文字をかえる」が選択されたら「this(=?)」に対して「slot_button_text_change()(=後で設定)」を実行させる
	m_customize->insertItem( codec->toUnicode("ダイアログをひらく"), this, SLOT(slot_show_dialog()) );  //項目「ダイアログを表示する」が選択されたら「this(=?)」に対して「slot_show_dialog()(=後で設定)」を実行させる
	QPopupMenu *m_menu = new QPopupMenu(this);  // メニュー項目「m_menu」を作成
	m_menu->insertItem( codec->toUnicode("終了する"), qApp, SLOT(quit()) );  //項目「終了する」が選択されたら「qApp(=アプリケーション)」に対して「quit()(=終了)」を実行させる
	
	// メニューバー「menubar」に項目を追加する。ここで追加した順に左から表示される。
	menubar->insertItem( codec->toUnicode("メニュー"), m_menu );  // メニューバー「menubar」に「m_menu」を「メニュー」という表示で追加
	menubar->insertItem( codec->toUnicode("カスタマイズ"), m_customize );  // メニューバーに「m_customize」を「カスタマイズ」という表示で追加
	menubar->setSeparator( QMenuBar::InWindowsStyle );  // ウィンドウズみたいなスタイル

	// 「button」が「clicked()」押されたら「qApp(=アプリケーション)」に対して「quit()(=終了)」を実行させる
	QObject::connect( button, SIGNAL(clicked()), qApp, SLOT(quit()) );

	// 「dialog.h」で組んだダイアログ「Dialog」を「dlg」として作成する
	dlg = new Dialog;
}


// スロット「slot_button_text_change()」を作成
void Mainwindow::slot_button_text_change()
{
	button -> setText(codec->toUnicode("終了する"));  // ボタン「button」の表示を「終了する」に設定する
}


// スロット「slot_show_dialog()」を作成
void Mainwindow::slot_show_dialog()
{
	label -> setText(codec->toUnicode("ダイアログをひらいています。"));  // ラベル「label」の表示を「ダイアログをひらいています。」に設定する
	dlg -> exec();  // ダイアログ「dlg(=Dialog)」を表示する
	
	// ダイアログ「dlg(=Dialog)」はモーダルダイアログなので、何らかの形でとじられない限りここから下のプログラムは実行されない
	
	// ダイアログのとじられ方によりラベルの表示を変える
	// dlg->result() はダイアログのとじられ方の結果を示す。0ならRejected(拒否)、1ならAccepted(許可)を示す。
	if( dlg->result() == 1 )  // <条件>もしダイアログのとじられ方が「1(=Accepted(OK))」なら
	{
		label -> setText(codec->toUnicode("ダイアログを「OK」か「とじる」でとじました。"));  // ラベル「label」の表示を「ダイアログを「OK」か「とじる」でとじました。」に設定する
	} else {  // <条件>そうでなければ
		label -> setText(codec->toUnicode("ダイアログを「X」でとじました。"));  // ラベル「label」の表示を「ダイアログを「X」でとじました。」に設定する
	}
}


(解説は、かきかけ)



<ヘッダーファイル名 dialog.h>

#ifndef DIALOG_H
#define DIALOG_H

#include <qmainwindow.h>   // 使用するクラスを指定する
#include <qapplication.h>
#include <qpushbutton.h>
#include <qtextcodec.h>
#include <qfont.h>
#include <qdialog.h>

class Dialog : public QDialog{
	Q_OBJECT

public:
	Dialog(QWidget *parent = 0, const char *name = 0, bool modal=TRUE);

private:
	QTextCodec *codec; // テキストコーデック
	QPushButton *button2; // ボタン
};

#endif //DIALOG_H


(解説は、かきかけ)



<CPPファイル名 dialog.cpp>

#include "dialog.h"  // dialog.cpp のヘッダーファイル dialog.h を読み込み

Dialog::Dialog(QWidget *parent, const char *name, bool modal=TRUE)
		: QDialog(parent, name, modal)
{
	// テキストコーデック「codec」を作成
	// 以降、codec->toUnicode("ここに書いたことは日本語で表示できる")
	codec = QTextCodec::codecForName("ShiftJIS");

	// ダイアログのタイトルを設定
	setCaption(codec->toUnicode("ダイアログ"));

	// 使用するフォントを設定
	QFont f("lcfont",18);
	setFont(f);

	// ボタン「button2」を作成(「Quit」と表示されたボタン)
	button2 = new QPushButton(codec->toUnicode("とじる"), this);
	// ボタン「button2」の配置位置を指定 setGeometry(配置x座標,配置y座標,ボタンの横幅,ボタンの縦幅)
	button2 -> setGeometry(200, 80, 70, 25);

	// 「button2」が「clicked()(=クリックした)」されたら「this(=?)」に対して「accept()(=許可と返して閉じる)」を実行させる
	QObject::connect( button2, SIGNAL(clicked()), this, SLOT(accept()) );
}


(解説は、かきかけ)




というわけで、さっそくコンパイルして実行してみましょう。
ここでは、「~/SLZaurus/」以下に「sample0003」フォルダを作成し、その中にソースがあることを前提にしています。


Cygwinを起動して、次のコマンドを実行してください。



. ~/SLZaurus/bin/dev-arm-qpe.sh
cd ~/SLZaurus/sample0003
progen -o sample0003.pro
tmake -o Makefile sample0003.pro
make



これでエラーが出なければ、「sample0003」というファイルがソースと同一のフォルダに作成されているので、それをリナザウに移してターミナルから実行してみてください。
エラーが出た場合は、もう一度ソースを見直して、問題のある個所を修正しましょう。問題のある個所は、「make」後のエラー情報でも最低限のことは提供されます。




ダイアログを表示できるようになったので、いろんなダイアログを作ってみてください。
プログラムもどんどん拡張してみてください。
条件<If〜>の使い方についても触れました。いろいろいじってみましょう。




<ソース>

sample0003.tar.gz




前へ ○○○ 「リナザウのソフトウェア開発」 ○○○ 次へ






inserted by FC2 system