13,144
回編集
| 349行目: | 349行目: | ||
== カスタムダイアログ == | == カスタムダイアログ == | ||
カスタムダイアログは、自由にレイアウト可能なダイアログである。<br> | |||
QDialogクラスを使用して作成することができる。<br> | |||
<br> | |||
以下の例では、メール送信用のダイアログを作成している。(ただし、レイアウトと結果の取得のみ対応)<br> | |||
<br> | |||
[OK]ボタンを押下する時、<code>accepted</code>シグナルが発行されて、[CANCEL]ボタンを押下する時、<code>rejected</code>シグナルが発行される。<br> | |||
各スロットは標準で用意されているため、任意の処理を記述せずに<code>1</code>または<code>0</code>を返す。<br> | |||
また、サイズグリップは表示されないが、ウインドウのサイズは自由に変更できる。<br> | |||
<syntaxhighlight lang="c++"> | |||
QFormLayout Form(); | |||
Form.setLabelAlignment(Qt::AlignRight); | |||
Form.addRow(tr("送信先 :"), new QLineEdit); | |||
Form.addRow(tr("件名 :"), new QLineEdit); | |||
Form.addRow(tr("本文 :"), new QTextEdit); | |||
QDialogButtonBox BtnBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal); | |||
BtnBox.button(QDialogButtonBox::Ok)->setText(tr("送信")); | |||
BtnBox.button(QDialogButtonBox::Cancel)->setText(tr("閉じる")); | |||
QVBoxLayout vbox; | |||
vbox.addLayout(Form); | |||
vbox.addWidget(BtnBox); | |||
QDialog Dlg(this); | |||
Dlg.setModal(true); | |||
Dlg.setWindowTitle(tr("メール送信")); | |||
Dlg.setLayout(vbox); | |||
connect(BtnBox, SIGNAL(accepted()), Dlg, SLOT(accept())); | |||
connect(BtnBox, SIGNAL(rejected()), Dlg, SLOT(reject())); | |||
// 1 : Ok(送信), 0 : Cancel(閉じる) | |||
int iRet = Dlg.exec(); | |||
</syntaxhighlight> | |||
<br> | |||
以下の例では、6つの選択ボタンを表示している。<br> | |||
<br> | |||
各ボタンにはプロパティを設定してボタン番号を設定する。(buttonNoは任意の名前である)<br> | |||
全てのボタンのイベントは、スロット<code>exitDialog</code>で処理する。<br> | |||
<br> | |||
最小サイズと最大サイズを同じ値に設定することで、ダイアログのサイズを固定している。<br> | |||
サイズグリップをオフにしても、サイズ変更不可にはならない。<br> | |||
<br> | |||
ダイアログ右上の[x]ボタンを押下した場合、buttonは0になる。<br> | |||
<syntaxhighlight lang="c++"> | |||
QStringList List; | |||
List << "グルメツアー" << "買い物ツアー" << "農業体験ツアー"; | |||
List << "工場見学" << "寺社巡り" << "歴史に触れる旅"; | |||
QLabel Label("コースを選択してください。"); | |||
Label.setFixedHeight(30); | |||
QGridLayout Grid; | |||
Grid.setContentsMargins(20, 16, 20, 24); // left, top, right, bottom | |||
Grid.setHorizontalSpacing(20); | |||
Grid.setVerticalSpacing(0); | |||
for(int i = 0; i < 3; i++) | |||
{ | |||
QLabel LabelCourse(QString((int)'A' + i) + " コース"); | |||
LabelCourse.setFixedHeight(20); | |||
Grid.addWidget(LabelCourse, 0, i, Qt::AlignHCenter); | |||
QPushButton Btn(List[i]); | |||
Btn.setFixedSize(150, 40); | |||
Btn.setStyleSheet("background:green; color:lightyellow;"); | |||
Btn.setProperty("buttonNo", i + 1); | |||
connect(&Btn, SIGNAL(clicked()), this, SLOT(exitDialog())); | |||
Grid.addWidget(&Btn, 1, i, Qt::AlignHCenter); | |||
} | |||
Grid.addWidget(new QLabel(""), 2, 0, 1, 3); | |||
for(int i = 0; i < 3; i++) | |||
{ | |||
QLabel *label = new QLabel(QString((int)'D' + i) + " コース"); | |||
label->setFixedHeight(20); | |||
Grid.addWidget(label, 3, i, Qt::AlignHCenter); | |||
QPushButton *pbtn = new QPushButton(list[i+3]); | |||
pbtn->setFixedSize(150, 40); | |||
pbtn->setStyleSheet("background:green; color:lightyellow;"); | |||
pbtn->setProperty("buttonNo", i + 3 + 1); | |||
connect(pbtn, SIGNAL(clicked()), this, SLOT(exitDialog())); | |||
Grid.addWidget(pbtn, 4, i, Qt::AlignHCenter); | |||
} | |||
QWidget Widget; | |||
Widget.setLayout(&Grid); | |||
Widget.setStyleSheet("background:lightyellow;"); | |||
QPushButton BtnQuit("どれも選ばない"); | |||
BtnQuit.setProperty("buttonNo", 0); | |||
connect(&BtnQuit, SIGNAL(clicked()), this, SLOT(exitDialog())); | |||
QVBoxLayout vbox; | |||
vbox.addWidget(Label); | |||
vbox.addWidget(Widget); | |||
vbox.addStretch(); | |||
vbox.addWidget(BtnQuit); | |||
QDialog Dlg(this); | |||
Dlg.setModal(true); | |||
Dlg.setWindowTitle(tr("ご案内")); | |||
Dlg.setSizeGripEnabled(false); | |||
Dlg.setMinimumSize(560, 290); | |||
Dlg.setMaximumSize(560, 290); | |||
Dlg.setLayout(vbox); | |||
// 0 : 選択しない, 1~6 : グルメ ~ 歴史に触れる | |||
int iRet = Dlg.exec(); | |||
</syntaxhighlight> | |||
<br> | |||
mainwindow.hファイルにスロットの宣言を記述する。<br> | |||
<syntaxhighlight lang="c++"> | |||
// mainwindow.h | |||
private slots: | |||
void exitDialog(); | |||
</syntaxhighlight> | |||
<br> | |||
スロットの処理を記述する。<br> | |||
<br> | |||
シグナルの送信元を調べて、QDialogであれば閉じる。<br> | |||
送信元のウインドウがQDialogではない場合、処理は実行しない。<br> | |||
buttonNoプロパティの値を取得して<code>done</code>メソッドを実行すると、ダイアログを閉じて<code>exec</code>メソッドの戻り値に変数iNoを渡す。<br> | |||
<syntaxhighlight lang="c++"> | |||
// mainwindow.cpp | |||
void MainWindow::exitDialog() | |||
{ | |||
QWidget *Window = static_cast<QWidget *>(sender())->window(); | |||
if(win->inherits("QDialog")) | |||
{ | |||
QDialog *Dlg = static_cast<QDialog *>(win); | |||
int iNo = (sender()->property("buttonNo")).toInt(); | |||
Dlg->done(iNo); | |||
} | |||
} | |||
</syntaxhighlight> | |||
<br><br> | <br><br> | ||