测试程序 界面:

main文件就省略了
//对话框头文件
// chapter2/builtin/src/builtindlg.h.
-
#ifndef _BUILTINDLG_H_
-
#define _BUILTINDLG_H_
-
#include <QtGui/QDialog>
-
class QTextEdit;
-
class QPushButton;
-
class CBuiltinDlg : public QDialog
-
{ -
Q_OBJECT
-
public:
-
CBuiltinDlg(QWidget* = 0);
-
virtual ~CBuiltinDlg() { }
-
private:
-
QTextEdit* displayTextEdit;
-
QPushButton* colorPushBtn;
-
QPushButton* errorPushBtn;
-
QPushButton* filePushBtn;
-
QPushButton* fontPushBtn;
-
QPushButton* inputPushBtn;
-
QPushButton* pagePushBtn;
-
QPushButton* progressPushBtn;
-
QPushButton* printPushBtn;
-
private slots:
-
void doPushBtn();
-
}; -
#endif
私有变量定义了8个按钮和一个文本编辑框.
还定义了一个私有槽,在其中做一些逻辑判断分别对不同的按钮进行响应.
//对话框的源文件
// chapter2/builtin/src/builtindlg.cpp.
-
#include <QtGui/QtGui>
-
#include "builtindlg.h"
-
CBuiltinDlg::CBuiltinDlg(QWidget* parent)
-
: QDialog(parent)
-
{ -
displayTextEdit = new QTextEdit(tr("Qt的标准通用对话框。"));
-
QGridLayout* gridLayout = new QGridLayout;
-
colorPushBtn = new QPushButton(tr("颜色对话框"));
-
errorPushBtn = new QPushButton(tr("错误信息框"));
-
filePushBtn = new QPushButton(tr("文件对话框"));
-
fontPushBtn = new QPushButton(tr("字体对话框"));
-
inputPushBtn = new QPushButton(tr("输入对话框"));
-
pagePushBtn = new QPushButton(tr("页设置对话框"));
-
progressPushBtn = new QPushButton(tr("进度对话框"));
-
printPushBtn = new QPushButton(tr("打印对话框"));
-
gridLayout->addWidget(colorPushBtn, 0, 0, 1, 1);
-
gridLayout->addWidget(errorPushBtn, 0, 1, 1, 1);
-
gridLayout->addWidget(filePushBtn, 0, 2, 1, 1);
-
gridLayout->addWidget(fontPushBtn, 1, 0, 1, 1);
-
gridLayout->addWidget(inputPushBtn, 1, 1, 1, 1);
-
gridLayout->addWidget(pagePushBtn, 1, 2, 1, 1);
-
gridLayout->addWidget(progressPushBtn, 2, 0, 1, 1);
-
gridLayout->addWidget(printPushBtn, 2, 1, 1, 1);
-
gridLayout->addWidget(displayTextEdit, 3, 0, 3, 3);
-
setLayout(gridLayout);
-
connect(colorPushBtn, SIGNAL(clicked()), this, SLOT(doPushBtn()));
-
connect(errorPushBtn, SIGNAL(clicked()), this, SLOT(doPushBtn()));
-
connect(filePushBtn, SIGNAL(clicked()), this, SLOT(doPushBtn()));
-
connect(fontPushBtn, SIGNAL(clicked()), this, SLOT(doPushBtn()));
-
connect(inputPushBtn, SIGNAL(clicked()), this, SLOT(doPushBtn()));
-
connect(pagePushBtn, SIGNAL(clicked()), this, SLOT(doPushBtn()));
-
connect(progressPushBtn, SIGNAL(clicked()), this, SLOT(doPushBtn()));
-
connect(printPushBtn, SIGNAL(clicked()), this, SLOT(doPushBtn()));
-
setWindowTitle(tr("內建对话框"));
-
resize(400, 300);
-
} -
void CBuiltinDlg::doPushBtn()
-
{ -
QPushButton* btn = qobject_cast<QPushButton*>(sender());
-
if(btn == colorPushBtn)
-
{// 颜色对话框.
-
QPalette palette = displayTextEdit->palette();
-
const QColor& color
-
= QColorDialog::getColor(palette.color(QPalette::Base), this);
-
if(color.isValid())
-
{
-
palette.setColor(QPalette::Base, color);
-
displayTextEdit->setPalette(palette);
-
}
-
}
-
else if(btn == errorPushBtn)
-
{// 错误信息框.
-
QErrorMessage box(this);
-
box.setWindowTitle(tr("错误信息框"));
-
box.showMessage(tr("错误信息框实例xx。"));
-
box.showMessage(tr("错误信息框实例xx。"));
-
box.showMessage(tr("错误信息框实例xx。"));
-
box.showMessage(tr("错误信息框实例yy。"));
-
box.showMessage(tr("错误信息框实例zz。"));
-
box.exec();
-
}
-
else if(btn == filePushBtn)
-
{// 文件对话框.
-
QString fileName = QFileDialog::getOpenFileName(this,
-
tr("打开文件"),
-
"/usr/local/Trolltech",
-
tr("任何文件(*.*)"
-
";;文本文件(*.txt)"
-
";;XML文件(*.xml)"));
-
displayTextEdit->setText(fileName);
-
}
-
else if(btn == fontPushBtn)
-
{// 字体对话框.
-
bool ok;
-
const QFont& font = QFontDialog::getFont(&ok,
-
displayTextEdit->font(),
-
this,
-
tr("字体对话框"));
-
if(ok)
-
{// 如果<确定>,设置字体.
-
displayTextEdit->setFont(font);
-
}
-
}
-
else if(btn == inputPushBtn)
-
{// 输入对话框.
-
bool ok;
-
QString text = QInputDialog::getText(this,
-
tr("输入对话框"),
-
tr("输入文本:"),
-
QLineEdit::Normal,
-
QDir::home().dirName(),
-
&ok);
-
if (ok && !text.isEmpty())
-
displayTextEdit->setText(text);
-
}
-
else if(btn == pagePushBtn)
-
{// 页设置对话框.
-
QPrinter printer;
-
QPageSetupDialog dlg(&printer, this);
-
dlg.setWindowTitle(tr("页设置对话框"));
-
if (dlg.exec() == QDialog::Accepted)
-
{
-
// 进行下一步的处理。
-
}
-
}
-
else if(btn == progressPushBtn)
-
{// 进度对话框.
-
QProgressDialog progress(tr("正在复制文件..."), tr("取消"), 0, 10000, this);
-
progress.setWindowModality(Qt::WindowModal);
-
progress.setWindowTitle(tr("进度对话框"));
-
progress.show();
-
for (int i = 0; i < 10000; i++)
-
{ -
progress.setValue(i);
-
qApp->processEvents();
-
if (progress.wasCanceled())
-
break;
-
//... 复制文件处理。
-
qDebug() << i;
-
}
-
progress.setValue(10000);
-
}
-
else if(btn == printPushBtn)
-
{// 打印对话框.
-
QPrinter printer;
-
QPrintDialog dlg(&printer, this);
-
dlg.setWindowTitle(tr("打印对话框"));
-
if (dlg.exec() == QDialog::Accepted)
-
{
-
// 进行下一步的处理。
-
}
-
}
源文件中只实现了两个函数,构造函数和按钮响应函数.
构造函数主要实现了,按钮和文本编辑框的创建,控件的布局,把所有的按钮的信号都连接到doPushBtn()槽上.
doPushBtn()函数比较重要的是如何判断按下的是哪个按钮.
sender()返回一个信号源指针, 然后给它投射成特定的指针qobject_cast<QPushButton *> 这样就有了发射按钮的指针.
本文详细介绍了如何使用Qt创建并实现一个包含多种标准通用对话框的内部对话框,包括颜色、错误信息、文件、字体、输入、页设置、进度和打印对话框。每个对话框的功能和响应机制都有具体的实现代码展示。

6740

被折叠的 条评论
为什么被折叠?



