Part 04 各类对话框(Qt)[2012.02.11]

 

Part 04 各类对话框(Qt)

——2012.02.11

0. 本次学习Qt时主要方法:根据QDialog的类继承关系,将绝大部分的对话框借助构造出。

1. 编写学习笔记主要形式:展示程序窗口,分析及总结程序设计思路,展示代码。

2. 主要参考学习资料: Qt Assistant.

3. 本Part内容:了解各类对话框的普通构造方法,初步认识各类对话框。

 

附:对话框类的继承关系图

 

010 Program– showSomeDialog

 

01.展示程序功能

这里是程序的主窗口,有一个主菜单,有五栏的工具栏,现在鼠标选中菜单栏中的第一项,在状态栏中有start to paint...的提示字样。

上面是QPrintDialog类的窗口,该窗口主要为打印功能。

 

现在鼠标选中了工具栏中的第二项,在状态栏中显示出“start to the color dialog box...”的字样。

上面是QColorDialog类的窗口,该窗口主要为颜色选择的功能。

 

现在鼠标选中了工具栏中的第三项,在状态栏中显示出“start to Error Message Dialog...”的字样。

 

上面是QErrorMessage类的窗口。

现在鼠标选中了工具栏中的第四项,在状态栏中显示出“start to file dialog... ”的字样。

 

上面是QFileDialog类的窗口。

 

现在鼠标选中了工具栏中的第五项,在状态栏中显示出“start to Font Dialog... ”的字样。

 

上面是QFontDialog类的窗口。

 

现在鼠标选中了工具栏中的第六项,在状态栏中显示出“start to input dialog... ”的字样。

 

上面是QInputDialog类的窗口。

现在鼠标选中了工具栏中的第七项,在状态栏中显示出“start to messagebox dialog... ”的字样。

 

上面是QMessageBox类的窗口。

 

现在鼠标选中了工具栏中的第九项,在状态栏中显示出“start to pageSetupDialog dialog... ”的字样。

 

上面是QPageSetupDialog类的窗口。

现在鼠标选中了工具栏中的第十项,在状态栏中显示出“start to the progress dialog box... ”的字样。

 

上面是QProgressDialog类的窗口。

 

 

02.从功能分析程序设计思路

 

          这个主窗口,看上去功能很多,实际上实现方式都一样:

          注:整理类设计思路:

                    第01步:本次设计,由于工具栏,状态栏,那些基本相同,所以就全部放在构造函数中实现。

                    第02 步:然后,再从构造函数中按功能划分出十个函数。

          注:分析功能中使用的统一的方法:

                    第03步:由于类中无其它特殊功能,十个函数可以采用同种实现方法。

                    第04步:如上一part所讲,先建动作,然后菜单栏,工具栏,连接,槽的实现。这是总体思路。

          注:本次学习的方法说难不难,说简单不简单:

                    第05步:对于每一个功能函数的实现,借助Qt Assistant查阅使用方法,对应编出即可。详细请看注解。

 

03. 程序代码

// main.cpp

#include "showsomedialog.h"
#include <QtGui/QApplication>

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	a.setWindowIcon(QIcon(":/showSomeDialog/Resources/main.png"));	// 设定主窗口图标
	showSomeDialog * w = new showSomeDialog;
	w -> adjustSize();
	w -> show();
	return a.exec();
}

// showsomedialog.h

#ifndef SHOWSOMEDIALOG_H
#define SHOWSOMEDIALOG_H

#include <QtGui/QMainWindow>

class showSomeDialog : public QMainWindow
{
	Q_OBJECT

public:
	showSomeDialog(QWidget *parent = 0, Qt::WFlags flags = 0);
	~showSomeDialog();

private slots:
	void printDialogSlot();
	void colorDialogSlot();
	void errorMessageSlot();
	void fileDialogSlot();
	void fontDialogSlot();
	void inputDialogSlot();
	void messageBoxSlot();
	void pageSetupDialogSlot();
	void progressDialogSlot();

private:
	void printDialog();
	void colorDialog();
	void errorMessage();
	void fileDialog();
	void fontDialog();
	void inputDialog();
	void messageBox();
	void pageSetupDialog();
	void progressDialog();
};

#endif // SHOWSOMEDIALOG_H

// showsomedialog.cpp

#include "showsomedialog.h"
#include <qaction.h>
#include <qmenu.h>
#include <qmenubar.h>
#include <qtoolbar.h>
#include <qstatusbar.h>
#include <qprintdialog.h>
#include <qcolordialog.h>
#include <qerrormessage.h>
#include <qfiledialog.h>
#include <qlabel.h>
#include <qfontdialog.h>
#include <qinputdialog.h>
#include <qmessagebox.h>
#include <qpagesetupdialog.h>
#include <qprogressdialog.h>

// 构造函数,初始化各个对话框函数
showSomeDialog::showSomeDialog(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
// 安装各对话框,按功能安装
	printDialog();
	this -> addToolBarBreak();
	colorDialog();
	this -> addToolBarBreak();
	errorMessage();
	fileDialog();
	fontDialog();
	inputDialog();
	messageBox();
	this -> addToolBarBreak();
	pageSetupDialog();
	this -> addToolBarBreak();
	progressDialog();
	this -> addToolBarBreak();
	
	QWidget::setFixedSize(700, 500); 
	statusBar();						// 安装状态栏
}

/**
 * QPrintDialog(打印对话框类)
 * The QPrintDialog class provides a dialog for specifying(指定) the printer's configuration(配置).
 * The dialog allows users to change document-related settings, 
 * such as the paper(页面) size(大小) and orientation(方向), type of print (color or grayscale), range of pages, and number of copies to print.
 * Controls are also provided to enable users to choose from the printers available, including any configured network printers.
 * Typically, QPrintDialog objects are constructed with a QPrinter object, and executed(生效) using the exec() function.
 **/
void showSomeDialog::printDialog()
{
	// 设置动作
	QAction * printDialog = new QAction(QObject::tr("&Print"), this);
	printDialog -> setIcon(QIcon(":/showSomeDialog/Resources/print.png"));
	printDialog -> setShortcut(QKeySequence::Print);
	printDialog -> setStatusTip(tr("start to paint..."));

	// 安装到菜单上
	QMenu * printDialogMenu = menuBar() -> addMenu(tr("Print"));
	printDialogMenu -> addAction(printDialog);

	// 安装到工具栏
	QToolBar * printDialogToolBar = addToolBar(tr("&Print"));
	printDialogToolBar -> addAction(printDialog);

	// 信号连接
	connect(printDialog, SIGNAL(triggered()), this, SLOT(printDialogSlot()) );

	return;
}

void showSomeDialog::printDialogSlot()
{
	QPrintDialog * print = new QPrintDialog(this);
	print -> open();
	return;
}


/**
 * QColorDialog (颜色对话框类)
 * The QColorDialog class provides a dialog widget for specifying colors.
 * The color dialog's function is to allow users to choose colors. 
 * For example, you might use this in a drawing program to allow the user to set the brush color.
 **/
void showSomeDialog::colorDialog()
{
	// 设置动作
	QAction * colorAction = new QAction(tr("Color"), this);
	colorAction -> setIcon(QIcon(":/showSomeDialog/Resources/color.png"));
	colorAction -> setStatusTip(tr("start to the color dialog box..."));

	// 设置菜单
	QMenu * colorMenu = menuBar() -> addMenu(tr("Color"));
	colorMenu -> addAction(colorAction);

	// 设置工具栏
	QToolBar * colorToolBar = addToolBar(tr("Color"));
	colorToolBar -> addAction(colorAction);

	// 信号连接
	connect(colorAction, SIGNAL(triggered()), this, SLOT(colorDialogSlot()));
	
	return;
}

void showSomeDialog::colorDialogSlot()
{
	QColorDialog * colorDialog = new QColorDialog;
	colorDialog -> open();
	return;
}


/**
 * QErrorMessage (错误信息提示类)
 * The QErrorMessage class provides an error message display dialog.
 * An error message widget consists of a text label and a checkbox. 
 * The checkbox lets the user control whether the same error message will be displayed again in the future, 
 * typically displaying the text, "Show this message again" translated into the appropriate local language.
 * For production applications, the class can be used to display messages which the user only needs to see once. 
 * To use QErrorMessage like this, you create the dialog in the usual way, 
 * and show it by calling the showMessage() slot or connecting signals to it.
 **/
void showSomeDialog::errorMessage()
{
	// 设置动作
	QAction * errorMessageAction = new QAction(tr("ErrorMessage"), this);
	errorMessageAction ->setIcon(QIcon(":/showSomeDialog/Resources/errorMessage.png"));
	errorMessageAction -> setStatusTip(tr("start to Error Message Dialog..."));

	// 设置菜单
	QMenu * errorMessageMenu = menuBar() -> addMenu(tr("ErrorMessage"));
	errorMessageMenu -> addAction(errorMessageAction);

	// 设置工具栏
	QToolBar * errorMessageMenuToolBar = addToolBar(tr("ErrorMessage"));
	errorMessageMenuToolBar -> addAction(errorMessageAction);
	
	// 信号连接
	connect (errorMessageAction, SIGNAL(triggered()), this, SLOT(errorMessageSlot()));

	return;
}
void showSomeDialog::errorMessageSlot()
{
	QErrorMessage * errorMessage = new QErrorMessage();
	errorMessage -> showMessage(tr("Here is testing..."));
	return;
}


/**
 * QFileDialog (文件选择对话框)
 * The QFileDialog class provides a dialog that allow users to select files or directories.
 * The QFileDialog class enables a user to traverse the file system in order to select one or many files or a directory.
 **/
void showSomeDialog::fileDialog()
{
	// 设置动作
	QAction * fileDialogAction = new QAction(tr("&File"), this);
	fileDialogAction -> setIcon(QIcon(":/showSomeDialog/Resources/file.png"));
	fileDialogAction -> setStatusTip(tr("start to file dialog..."));
	fileDialogAction -> setShortcut(QKeySequence::Open);

	// 设置菜单
	QMenu * fileDialogMenu = menuBar() -> addMenu(tr("&File"));
	fileDialogMenu -> addAction(fileDialogAction);

	// 设置工具栏
	QToolBar * fileDialogToolBar = addToolBar(tr("&File"));
	fileDialogToolBar -> addAction(fileDialogAction);

	// 连接
	connect (fileDialogAction, SIGNAL(triggered()), this, SLOT(fileDialogSlot()) );

}
void showSomeDialog::fileDialogSlot()
{
	QFileDialog * fileDialog = new QFileDialog(this, Qt::Window);
	fileDialog -> open();
	return;
}


/**
 * QFontDialog (字体对话框)
 * The QFontDialog class provides a dialog widget for selecting a font.
 * A font dialog is created through one of the static getFont() functions.
 **/
void showSomeDialog::fontDialog()
{
	// 设置动作
	QAction * fontDialogAction = new QAction(tr("&font"), this);
	fontDialogAction -> setIcon(QIcon(":/showSomeDialog/Resources/font.png"));
	fontDialogAction -> setStatusTip(tr("start to font dialog..."));

	// 设置菜单
	QMenu * fontDialogMenu = menuBar() -> addMenu(tr("&font"));
	fontDialogMenu -> addAction(fontDialogAction);

	// 设置工具栏
	QToolBar * fontDialogToolBar = addToolBar(tr("&font"));
	fontDialogToolBar -> addAction(fontDialogAction);

	// 连接
	connect (fontDialogAction, SIGNAL(triggered()), this, SLOT(fontDialogSlot()) );

	return;
}
void showSomeDialog::fontDialogSlot()
{
	QFontDialog * fontDialog = new QFontDialog();
	fontDialog -> open();
	return;
}


/**
 * inputDiaglog (输入对话框)
 * The QInputDialog class provides a simple convenience dialog to get a single value from the user.
 * The input value can be a string, a number or an item from a list. A label must be set to tell the user what they should enter.
 **/
void showSomeDialog::inputDialog()
{
	// 设置动作
	QAction * inputDialogAction = new QAction(tr("&input"), this);
	inputDialogAction -> setIcon(QIcon(":/showSomeDialog/Resources/input.png"));
	inputDialogAction -> setStatusTip(tr("start to input dialog..."));

	// 设置菜单
	QMenu * inputDialogMenu = menuBar() -> addMenu(tr("&input"));
	inputDialogMenu -> addAction(inputDialogAction);

	// 设置工具栏
	QToolBar * inputDialogToolBar = addToolBar(tr("&input"));
	inputDialogToolBar -> addAction(inputDialogAction);

	// 连接
	connect (inputDialogAction, SIGNAL(triggered()), this, SLOT(inputDialogSlot()) );

	return;
}
void showSomeDialog::inputDialogSlot()
{
	QInputDialog * inputDialog = new QInputDialog();
	inputDialog -> setInputMode(QInputDialog::TextInput);
	inputDialog -> open();
	return;
}


/**
 * QMessageBox(信息提示选择对话框)
 * The QMessageBox class provides a modal dialog for informing the user or for asking the user a question and receiving an answer.
 * A message box displays a primary text to alert the user to a situation,
 * an informative text to further explain the alert or to ask the user a question, 
 * and an optional detailed text to provide even more data if the user requests it.
 * A message box can also display an icon and standard buttons for accepting a user response.
 * Two APIs for using QMessageBox are provided, the property-based API, and the static functions. 
 * Calling one of the static functions is the simpler approach, but it is less flexible than using the property-based API, 
 * and the result is less informative. Using the property-based API is recommended.
 **/
void showSomeDialog::messageBox()
{
	// 设置动作
	QAction * messageboxDialogAction = new QAction(tr("&messagebox"), this);
	messageboxDialogAction -> setIcon(QIcon(":/showSomeDialog/Resources/messagebox.png"));
	messageboxDialogAction -> setStatusTip(tr("start to messagebox dialog..."));

	// 设置菜单
	QMenu * messageboxDialogMenu = menuBar() -> addMenu(tr("&messagebox"));
	messageboxDialogMenu -> addAction(messageboxDialogAction);

	// 设置工具栏
	QToolBar * messageboxDialogToolBar = addToolBar(tr("&messagebox"));
	messageboxDialogToolBar -> addAction(messageboxDialogAction);

	// 连接
	connect (messageboxDialogAction, SIGNAL(triggered()), this, SLOT(messageBoxSlot()) );

	return;
}
void showSomeDialog::messageBoxSlot()
{
	QMessageBox messageBox;
	messageBox.setIcon(QMessageBox::Information);
	messageBox.setText("setTitleText");
	messageBox.setInformativeText("setInformativeText");
	messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
	messageBox.setDefaultButton(QMessageBox::Yes);
	messageBox.exec();
	return;
}


/**
 * QPageSetupDialog
 * The QPageSetupDialog class provides a configuration dialog for the page-related options on a printer.
 **/
void showSomeDialog::pageSetupDialog()
{
	// 设置动作
	QAction * pageSetupDialogDialogAction = new QAction(tr("&pageSetupDialog"), this);
	pageSetupDialogDialogAction -> setIcon(QIcon(":/showSomeDialog/Resources/pageSetupDialog.png"));
	pageSetupDialogDialogAction -> setStatusTip(tr("start to pageSetupDialog dialog..."));

	// 设置菜单
	QMenu * pageSetupDialogDialogMenu = menuBar() -> addMenu(tr("&pageSetupDialog"));
	pageSetupDialogDialogMenu -> addAction(pageSetupDialogDialogAction);

	// 设置工具栏
	QToolBar * pageSetupDialogDialogToolBar = addToolBar(tr("&pageSetupDialog"));
	pageSetupDialogDialogToolBar -> addAction(pageSetupDialogDialogAction);

	// 连接
	connect (pageSetupDialogDialogAction, SIGNAL(triggered()), this, SLOT(pageSetupDialogSlot()) );

	return;
}
void showSomeDialog::pageSetupDialogSlot()
{
	QPageSetupDialog * pageSetupDialog = new QPageSetupDialog;
	pageSetupDialog -> open();
	return;
}


/**
 * QProgressDialog
 * The QProgressDialog class provides feedback(反馈) on the progress of a slow operation.
 * A progress dialog is used to give the user an indication(指示) of how long an operation is going to take, 
 * and to demonstrate(演示) that the application has not frozen(停止运作). 
 * It can also give the user an opportunity to abort the operation.
 * A common problem with progress dialogs is that it is difficult to know when to use them;
 * operations take different amounts of time on different hardware. (不同硬件运行需要不同时间)
 * QProgressDialog offers a solution to this problem: (下面是该dialog的解决方法)
 * it estimates the time the operation will take (based on time for steps), 
 * and only shows itself if that estimate is beyond minimumDuration() (4 seconds by default).
 * Use setMinimum() and setMaximum() or the constructor to set the number of "steps" in the operation and call setValue() as the operation progresses. 
 * The number of steps can be chosen arbitrarily.(通过两个函数设定步骤和通过setValue()对进度进行反馈,步骤的数量可以任意选择) 
 * It can be the number of files copied, the number of bytes received, the number of iterations(反复) through the main loop of your algorithm, 
 * or some other suitable unit. Progress starts at the value set by setMinimum(), 
 * and the progress dialog shows that the operation has finished when you call setValue() with the value set by setMaximum() as its argument.
 * The dialog automatically resets and hides itself at the end of the operation.(进程至最大值时,它将自动隐藏)
 * Use setAutoReset() and setAutoClose() to change this behavior. 
 * Note that if you set a new maximum (using setMaximum() or setRange()) that equals your current value(), the dialog will not close regardless.(重设最大值后会根据最大值调整关闭窗口)
 * There are two ways of using QProgressDialog: modal and modeless.(模态和非模态)
 * Compared to a modeless QProgressDialog, a modal QProgressDialog is simpler to use for the programmer. 
 * Do the operation in a loop, call setValue() at intervals, and check for cancellation with wasCanceled(). 
 **/
void showSomeDialog::progressDialog()
{
	// 添加动作
	QAction * progressDialogAction = new QAction(QIcon(":/showSomeDialog/Resources/progress.png"), tr("progressDialog"), this);
	progressDialogAction -> setStatusTip(tr("start to open the progress dialog box... "));

	// 设置菜单
	QMenu * progressDialogMenu = menuBar() -> addMenu(tr("progressDialog"));
	progressDialogMenu -> addAction(progressDialogAction);

	// 设置工具栏
	QToolBar * progressDialogToolBar = addToolBar(tr("progressDialog"));
	progressDialogToolBar -> addAction(progressDialogAction);

	// 信号/槽连接
	connect(progressDialogAction, SIGNAL(triggered()), this, SLOT(progressDialogSlot()));
}
void showSomeDialog::progressDialogSlot()
{
	QProgressDialog * progressDialog = new QProgressDialog(tr("labelText"), tr("cancelButtonText"), 1, 100);
	progressDialog -> open();
	int i(1);
	while (i++ != 99)
	{
		progressDialog -> setValue(i);
	}
	return;
}

// 析构函数
showSomeDialog::~showSomeDialog()
{

}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值