Qt QColorDialog QErrorMessage QFileDialog QFontDialog QProgressDialog 等对话框的使用测试

测试程序 界面:

Qt QColorDialog QErrorMessage QFileDialog QFontDialog QProgressDialog 等对话框的使用测试_第1张图片

main文件就省略了

//对话框头文件

// chapter2/builtin/src/builtindlg.h.

  1. #ifndef _BUILTINDLG_H_
  2. #define _BUILTINDLG_H_
  3. #include <QtGui/QDialog>
  4. class QTextEdit;
  5. class QPushButton;
  6. class CBuiltinDlg : public QDialog
  7. {
  8.  	Q_OBJECT
  9. public:
  10. 	CBuiltinDlg(QWidget* = 0);
  11. 	virtual ~CBuiltinDlg() { }	
  12. private:
  13. 	QTextEdit*	displayTextEdit;
  14. 	QPushButton*	colorPushBtn;
  15. 	QPushButton*	errorPushBtn;
  16. 	QPushButton*	filePushBtn;
  17. 	QPushButton*	fontPushBtn;
  18. 	QPushButton*	inputPushBtn;
  19. 	QPushButton*	pagePushBtn;
  20. 	QPushButton*	progressPushBtn;
  21. 	QPushButton*	printPushBtn;	
  22. private slots:
  23. 	void doPushBtn();
  24. };
  25. #endif

私有变量定义了8个按钮和一个文本编辑框.

还定义了一个私有槽,在其中做一些逻辑判断分别对不同的按钮进行响应.

//对话框的源文件

// chapter2/builtin/src/builtindlg.cpp.

  1. #include <QtGui/QtGui>
  2. #include "builtindlg.h"
  3.  
  4. CBuiltinDlg::CBuiltinDlg(QWidget* parent)
  5.  :	QDialog(parent)
  6. {
  7. 	displayTextEdit = new QTextEdit(tr("Qt的标准通用对话框。"));
  8.  
  9. 	QGridLayout* gridLayout = new QGridLayout;
  10. 	colorPushBtn 	= new QPushButton(tr("颜色对话框"));
  11. 	errorPushBtn 	= new QPushButton(tr("错误信息框"));
  12. 	filePushBtn 	= new QPushButton(tr("文件对话框"));
  13. 	fontPushBtn 	= new QPushButton(tr("字体对话框"));
  14. 	inputPushBtn 	= new QPushButton(tr("输入对话框"));
  15. 	pagePushBtn 	= new QPushButton(tr("页设置对话框"));
  16. 	progressPushBtn 	= new QPushButton(tr("进度对话框"));
  17. 	printPushBtn	= new QPushButton(tr("打印对话框"));
  18. 	gridLayout->addWidget(colorPushBtn, 0, 0, 1, 1);
  19. 	gridLayout->addWidget(errorPushBtn, 0, 1, 1, 1);
  20. 	gridLayout->addWidget(filePushBtn, 0, 2, 1, 1);
  21. 	gridLayout->addWidget(fontPushBtn, 1, 0, 1, 1);
  22. 	gridLayout->addWidget(inputPushBtn, 1, 1, 1, 1);
  23. 	gridLayout->addWidget(pagePushBtn, 1, 2, 1, 1);
  24. 	gridLayout->addWidget(progressPushBtn, 2, 0, 1, 1);
  25. 	gridLayout->addWidget(printPushBtn, 2, 1, 1, 1);
  26. 	gridLayout->addWidget(displayTextEdit, 3, 0, 3, 3);
  27.  
  28. 	setLayout(gridLayout);
  29.  
  30.  	connect(colorPushBtn, SIGNAL(clicked()), this, SLOT(doPushBtn()));
  31.  	connect(errorPushBtn, SIGNAL(clicked()), this, SLOT(doPushBtn()));
  32. 	connect(filePushBtn, SIGNAL(clicked()), this, SLOT(doPushBtn()));
  33.  	connect(fontPushBtn, SIGNAL(clicked()), this, SLOT(doPushBtn()));
  34. 	connect(inputPushBtn, SIGNAL(clicked()), this, SLOT(doPushBtn()));
  35.  	connect(pagePushBtn, SIGNAL(clicked()), this, SLOT(doPushBtn()));
  36. 	connect(progressPushBtn, SIGNAL(clicked()), this, SLOT(doPushBtn()));
  37. 	connect(printPushBtn, SIGNAL(clicked()), this, SLOT(doPushBtn()));
  38. 	setWindowTitle(tr("內建对话框"));
  39. 	resize(400, 300);
  40. }
  41. void CBuiltinDlg::doPushBtn()
  42. {
  43. 	QPushButton* btn = qobject_cast<QPushButton*>(sender());
  44. 	if(btn == colorPushBtn)
  45. 	{// 颜色对话框.
  46. 		QPalette palette = displayTextEdit->palette();	
  47. 		const QColor& color 
  48. 			= QColorDialog::getColor(palette.color(QPalette::Base), this);
  49. 		if(color.isValid())
  50. 		{
  51. 			palette.setColor(QPalette::Base, color);
  52. 			displayTextEdit->setPalette(palette);	
  53. 		}
  54. 	}
  55. else if(btn == errorPushBtn)
  56. 	{// 错误信息框.
  57. 		QErrorMessage box(this);
  58. 		box.setWindowTitle(tr("错误信息框"));
  59. 		box.showMessage(tr("错误信息框实例xx。"));
  60. 		box.showMessage(tr("错误信息框实例xx。"));
  61. 		box.showMessage(tr("错误信息框实例xx。"));
  62. 		box.showMessage(tr("错误信息框实例yy。"));
  63. 		box.showMessage(tr("错误信息框实例zz。"));
  64. 		box.exec();
  65. 	}
  66. 	else if(btn == filePushBtn)
  67. 	{// 文件对话框.
  68. 		QString fileName = QFileDialog::getOpenFileName(this, 
  69. 						            tr("打开文件"),
  70.                                                                    "/usr/local/Trolltech",
  71.                                                                    tr("任何文件(*.*)"
  72.                                                                      ";;文本文件(*.txt)"
  73. 													";;XML文件(*.xml)"));
  74. 		displayTextEdit->setText(fileName);
  75. 	}
  76. 	else if(btn == fontPushBtn)
  77. 	{// 字体对话框.
  78. 		bool ok;
  79. 		const QFont& font = QFontDialog::getFont(&ok,
  80. 									displayTextEdit->font(),
  81. 									this,
  82. 									tr("字体对话框"));
  83. 		if(ok)
  84. 		{// 如果<确定>,设置字体.
  85. 			displayTextEdit->setFont(font);
  86. 		}
  87. 	}
  88. 	else if(btn == inputPushBtn)
  89. 	{// 输入对话框.
  90. 		bool ok;
  91.      	QString text = QInputDialog::getText(this, 
  92. 											tr("输入对话框"),
  93.                                           	tr("输入文本:"), 
  94. 											QLineEdit::Normal,
  95.                                           	QDir::home().dirName(), 
  96. &ok);
  97.      if (ok && !text.isEmpty())
  98.          displayTextEdit->setText(text);
  99. 	}
  100. 	else if(btn == pagePushBtn)
  101. 	{// 页设置对话框.
  102. 		QPrinter printer;
  103. 		QPageSetupDialog dlg(&printer, this);
  104. 		dlg.setWindowTitle(tr("页设置对话框"));
  105. 		if (dlg.exec() == QDialog::Accepted) 
  106. 		{
  107. 			// 进行下一步的处理。
  108.  		}	
  109. 	} 
  110. 	else if(btn == progressPushBtn)
  111. 	{// 进度对话框.
  112. 		QProgressDialog progress(tr("正在复制文件..."), tr("取消"), 0, 10000, this);
  113. 		progress.setWindowModality(Qt::WindowModal);
  114. 		progress.setWindowTitle(tr("进度对话框"));
  115. 		progress.show();
  116. 		for (int i = 0; i < 10000; i++) 
  117. {
  118. 			progress.setValue(i);
  119. 			qApp->processEvents();	
  120. 			if (progress.wasCanceled())
  121. 				break;
  122. 			//... 复制文件处理。
  123. 			qDebug() << i;
  124. 		}
  125. 		progress.setValue(10000);
  126. 	}
  127.  
  128. 	else if(btn == printPushBtn)
  129. 	{// 打印对话框.
  130. 		QPrinter printer;
  131. 		QPrintDialog dlg(&printer, this);
  132. 		dlg.setWindowTitle(tr("打印对话框"));
  133. 		if (dlg.exec() == QDialog::Accepted) 
  134. 		{
  135. 			// 进行下一步的处理。
  136.  		}
  137. 	}
源文件中只实现了两个函数,构造函数和按钮响应函数.
构造函数主要实现了,按钮和文本编辑框的创建,控件的布局,把所有的按钮的信号都连接到doPushBtn()槽上.
doPushBtn()函数比较重要的是如何判断按下的是哪个按钮.
sender()返回一个信号源指针,  然后给它投射成特定的指针qobject_cast<QPushButton *> 这样就有了发射按钮的指针.
 

你可能感兴趣的:(xml,测试,qt,文本编辑,Signal)