Qt標準ダイアログの概要
Qtフレームワークでは、開発者が頻繁に利用するダイアログ機能が標準で提供されています。これらのダイアログはすべてQDialogクラスを継承しており、統一された使用方法で実装できます。
メッセージダイアログの活用
メッセージダイアログはアプリケーションにおいて最も重要なUI要素の一つです。主な用途は以下の通りです:
- 重要な情報をユーザーに通知する
- ユーザーに操作選択を強制する場面で使用
QMessageBoxには様々な実装用ヘルパー関数が用意されており、複数のパラメータを一度に設定できます。
ファイルダイアログの実装
ファイルダイアログは以下のような場面で活用されます:
- オープンモード:外部ファイルを読み込む際
- 保存モード:アプリケーションデータを外部ファイルに保存する際
ファイルフィルター機能により、拡張子に基づいてファイルを絞り込むことができます。フィルターの定義は特定のルールに従います。
QFileDialogには多くの実用関数が提供されています。
サンプルコード
#ifndef DIALOGWINDOW_H
#define DIALOGWINDOW_H
#include <QtGui/QWidget>
#include <QPushButton>
class DialogWindow : public QWidget
{
Q_OBJECT
private:
QPushButton infoButton;
QPushButton detailButton;
QPushButton loadButton;
QPushButton storeButton;
private slots:
void showInfoDialog();
void showDetailDialog();
void showLoadDialog();
void showStoreDialog();
public:
DialogWindow(QWidget *parent = nullptr);
~DialogWindow();
};
#endif // DIALOGWINDOW_H
DialogWindow.h
#include "DialogWindow.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QDebug>
DialogWindow::DialogWindow(QWidget *parent)
: QWidget(parent), infoButton(this), detailButton(this),
loadButton(this), storeButton(this)
{
infoButton.setText("基本メッセージ");
infoButton.move(20, 20);
infoButton.resize(160, 30);
detailButton.setText("詳細メッセージ");
detailButton.move(20, 70);
detailButton.resize(160, 30);
loadButton.setText("ファイルを開く");
loadButton.move(20, 120);
loadButton.resize(160, 30);
storeButton.setText("ファイルを保存");
storeButton.move(20, 170);
storeButton.resize(160, 30);
resize(200, 200);
setFixedSize(200, 220);
connect(&infoButton, SIGNAL(clicked()), this, SLOT(showInfoDialog()));
connect(&detailButton, SIGNAL(clicked()), this, SLOT(showDetailDialog()));
connect(&loadButton, SIGNAL(clicked()), this, SLOT(showLoadDialog()));
connect(&storeButton, SIGNAL(clicked()), this, SLOT(showStoreDialog()));
}
void DialogWindow::showInfoDialog()
{
QMessageBox notification(this);
notification.setText("基本通知メッセージ");
notification.exec();
}
void DialogWindow::showDetailDialog()
{
QMessageBox detailedMsg(this);
detailedMsg.setWindowTitle("詳細通知");
detailedMsg.setText("これは詳細情報を含むメッセージです");
detailedMsg.setIcon(QMessageBox::Information);
detailedMsg.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel | QMessageBox::Close);
if(detailedMsg.exec() == QMessageBox::Ok)
{
qDebug() << "OKが選択されました";
}
}
void DialogWindow::showLoadDialog()
{
QFileDialog fileSelector(this);
fileSelector.setAcceptMode(QFileDialog::AcceptOpen);
fileSelector.setNameFilter("画像 (*.png *.xpm *.jpg);;テキスト (*.txt);;XML (*.xml);;全ファイル(*.*)");
fileSelector.setFileMode(QFileDialog::ExistingFiles);
if(fileSelector.exec() == QFileDialog::Accepted)
{
QStringList filePaths = fileSelector.selectedFiles();
for(const QString& path : filePaths)
{
qDebug() << "選択されたファイル:" << path;
}
}
}
void DialogWindow::showStoreDialog()
{
QFileDialog fileSaver(this);
fileSaver.setAcceptMode(QFileDialog::AcceptSave);
fileSaver.setNameFilter("画像 (*.png *.xpm *.jpg);;テキスト (*.txt);;XML (*.xml);;全ファイル(*.*)");
fileSaver.setFileMode(QFileDialog::AnyFile);
if(fileSaver.exec() == QFileDialog::Accepted)
{
QStringList savePaths = fileSaver.selectedFiles();
for(const QString& path : savePaths)
{
qDebug() << "保存先:" << path;
}
}
}
DialogWindow::~DialogWindow()
{
}
DialogWindow.cpp
#include <QtGui/QApplication>
#include "DialogWindow.h"
int main(int argc, char *argv[])
{
QApplication application(argc, argv);
DialogWindow window;
window.show();
return application.exec();
}
main.cpp
まとめ
- Qtでは再利用可能な複数のダイアログタイプが提供されている
- すべての標準ダイアログはQDialogを継承し、統一された使用方法を持つ
- QMessageBoxは重要なプログラム情報の提示に使用される
- QFileDialogはシステム内のファイルパス取得に活用される