您的位置:首页 > 编程语言 > Qt开发

qt学习笔记(四)之对话框-Qt内建对话框 .

2013-06-18 11:34 453 查看
来源 /article/10861743.html

在上一节中,我们已经清楚的了解到QDialog的子类,像QColorDialog,QFontDiog等,这些对话框我们称之为内建对话框,又叫标准对话框



这一节我们主要讲解下标准对话框的一些使用方法。

一、下图为我们要设计出来的主界面



窗口的绘制,这里就不在叙述了。

特别说明下:窗口的绘制我没有使用布局管理器,而是自己“设计”的布局。

[cpp]
view plaincopyprint?

resize(320, 240);

resize(320, 240);


首先我调用QWidget::resize(intw,inth)
来设置对话框窗口大小。

然后设计其他部件格局

[cpp]
view plaincopyprint?

//自定义布局
infoTextEdit->setGeometry(QRect(20, 130, 280, 100));
colorBtn->setGeometry(QRect(20 , 10, 80, 30));
errorBtn->setGeometry(QRect(120 , 10, 80, 30));
fileBtn->setGeometry(QRect(220 , 10, 80, 30));
fontBtn->setGeometry(QRect(20 , 50, 80, 30));
inputBtn->setGeometry(QRect(120 , 50, 80, 30));
pageBtn->setGeometry(QRect(220 , 50, 80, 30));
progressBtn->setGeometry(QRect(20 , 90, 80, 30));
printBtn->setGeometry(QRect(120 , 90, 80, 30));

//自定义布局
infoTextEdit->setGeometry(QRect(20, 130, 280, 100));
colorBtn->setGeometry(QRect(20 , 10, 80, 30));
errorBtn->setGeometry(QRect(120 , 10, 80, 30));
fileBtn->setGeometry(QRect(220 , 10, 80, 30));
fontBtn->setGeometry(QRect(20 , 50, 80, 30));
inputBtn->setGeometry(QRect(120 , 50, 80, 30));
pageBtn->setGeometry(QRect(220 , 50, 80, 30));
progressBtn->setGeometry(QRect(20 , 90, 80, 30));
printBtn->setGeometry(QRect(120 , 90, 80, 30));

主要是调用了QWidget::setGeometry(intax,intay,intaw,intah)

当然,我们还要对按钮进行“信号与槽”编辑

可以设计一个槽函数,然后再内部进行按钮对象的识别。

当信号被激活,应用程序会进入槽函数。我们就可以根据sender()来进行发送信号的对象

[cpp]
view plaincopyprint?

QPushButton *btn = qobject_cast<QPushButton *>(sender());

QPushButton *btn = qobject_cast<QPushButton *>(sender());



T qobject_cast ( QObject * object )

完成类型的转换,将<QObject *>类型的对象指针转换为类型为<T *>的对象指针,如果转换成功,返回正确的对象指针,否则返回0。

------------------------------------------------

Returns the given object cast to type T if the object is of type T (or of a subclass); otherwise returns 0. If object is 0 then it will also return 0.

The class T must inherit (directly or indirectly) QObject and
be declared with the Q_OBJECT macro.

------------------------------------------------

类型T必须直接继承或者间接继承QObject 类,并且在该类的定义里有Q_OBJECT宏变量(否则qobject_cast返回值是未定义的)

二、颜色对话框



下面的代码是关于颜色对话框的应用

[cpp]
view plaincopyprint?

QPalette palette = infoTextEdit->palette(); //获取文本编辑的调色板对象

//QColor color = QColorDialog::getColor(QPalette::Base, this);

//颜色对话框的初始颜色值为文本编辑的背景色

QColor color = QColorDialog::getColor(palette.color(QPalette::Base), this);
//如果用户在颜色对话框点击取消的话,得到的color是无效的

if(color.isValid())
{
//QPalette::Base 通常用于背景色
palette.setColor(QPalette::Base, color);
infoTextEdit->setPalette(palette);
}

QPalette palette = infoTextEdit->palette(); //获取文本编辑的调色板对象
//QColor color = QColorDialog::getColor(QPalette::Base, this);
//颜色对话框的初始颜色值为文本编辑的背景色
QColor color = QColorDialog::getColor(palette.color(QPalette::Base), this);
//如果用户在颜色对话框点击取消的话,得到的color是无效的
if(color.isValid())
{
//QPalette::Base 通常用于背景色
palette.setColor(QPalette::Base, color);
infoTextEdit->setPalette(palette);
}

这里我们用上了调色板,调色板可以用来设置窗口部件、按钮窗口部件、文本输入窗口部件的背景色和前景色,通过ColorRole来指定,在Qt的文档中都有说明。

三、错误消息框



[cpp]
view plaincopyprint?

QErrorMessage msgbox(this);

//这里主要是为了区别错误消息框中的”再次显示消息“

msgbox.setWindowTitle(tr("错误消息框"));
msgbox.showMessage(tr("错误消息框1"));
msgbox.showMessage(tr("错误消息框1"));
msgbox.showMessage(tr("错误消息框1"));
msgbox.showMessage(tr("错误消息框2"));
msgbox.showMessage(tr("错误消息框3"));
msgbox.exec();

QErrorMessage msgbox(this);

//这里主要是为了区别错误消息框中的”再次显示消息“
msgbox.setWindowTitle(tr("错误消息框"));
msgbox.showMessage(tr("错误消息框1"));
msgbox.showMessage(tr("错误消息框1"));
msgbox.showMessage(tr("错误消息框1"));
msgbox.showMessage(tr("错误消息框2"));
msgbox.showMessage(tr("错误消息框3"));
msgbox.exec();

我们可以创建一个QErrorMessage对象,然后分别设置显示内容,最后让该对话框执行。

如果我们不用exec(),而是直接show(),这时候会发现错误消息框一闪而过。

因为msgbox的生存周期在show()完就结束了,所以我们还是调用exec()。应用程序会在用户关闭消息对话框后,才return 出来。

这样讲很不恰当~~语言能力有限,哎~~只能这么解释了。

四、文件对话框



[cpp]
view plaincopyprint?

QString FileName = QFileDialog::getOpenFileName(this, tr("打开文件"), "/home", tr("任何文件(*.c)"));

infoTextEdit->setText(FileName);

QString FileName = QFileDialog::getOpenFileName(this, tr("打开文件"), "/home", tr("任何文件(*.c)"));

infoTextEdit->setText(FileName);

文件对话框的创建比较简单.QFileDialog::getOpenFileName()第三个参数为设定初始默认路径,第四个参数是过滤器。可以这么说:过滤器指定的文件格式才有效。

五、字体对话框



[cpp]
view plaincopyprint?

bool ok;
QFont font = QFontDialog::getFont(&ok, infoTextEdit->font(), this, tr("字体对话框"));
if(ok)
{
infoTextEdit->setFont(font);
}

bool ok;
QFont font = QFontDialog::getFont(&ok, infoTextEdit->font(), this, tr("字体对话框"));
if(ok)
{
infoTextEdit->setFont(font);
}

当我们选择好要设置的字体格式,点击OK后,bool ok的值为true。

六、输入对话框



[cpp]
view plaincopyprint?

bool ok;
QString text = QInputDialog::getText(this, tr("输入对话框"), tr("输入文本"), QLineEdit::Normal, tr("fuck"), &ok);

//判断
if(ok && !text.isEmpty())
{
infoTextEdit->setText(text);
}

bool ok;
QString text = QInputDialog::getText(this, tr("输入对话框"), tr("输入文本"), QLineEdit::Normal, tr("fuck"), &ok);

//判断
if(ok && !text.isEmpty())
{
infoTextEdit->setText(text);
}

输入对话框就不介绍了,我们也可以定义自己的输入对话框类。

七、页面设置对话框



[cpp]
view plaincopyprint?

QPrinter printer;
QPageSetupDialog pageDlg(&printer, this);
pageDlg.setWindowTitle(tr("页面设置对话框"));
if(pageDlg.exec() == QDialog::Accepted)
{
//下一步处理
}

QPrinter printer;
QPageSetupDialog pageDlg(&printer, this);
pageDlg.setWindowTitle(tr("页面设置对话框"));
if(pageDlg.exec() == QDialog::Accepted)
{
//下一步处理
}

我们都可以查看Qt的帮助文档,来完成这些程序设计。

八、进度对话框



[cpp]
view plaincopyprint?

QProgressDialog progressDlg(tr("正在复制文件"), tr("取消"), 0, 10000, this);
progressDlg.setWindowTitle(tr("进度条对话框"));
//设置为模态对话框
progressDlg.setWindowModality(Qt::WindowModal);
//如果这里使用exec 进度对话框会卡主
progressDlg.show();
for(int i = 0; i < 10000; i++)
{
progressDlg.setValue(i);
qApp->processEvents();

if(progressDlg.wasCanceled())
{
break;
}
}
progressDlg.setValue(10000);

QProgressDialog progressDlg(tr("正在复制文件"), tr("取消"), 0, 10000, this);
progressDlg.setWindowTitle(tr("进度条对话框"));
//设置为模态对话框
progressDlg.setWindowModality(Qt::WindowModal);
//如果这里使用exec 进度对话框会卡主
progressDlg.show();
for(int i = 0; i < 10000; i++)
{
progressDlg.setValue(i);
qApp->processEvents();

if(progressDlg.wasCanceled())
{
break;
}
}
progressDlg.setValue(10000);

我们可以通过调用QProgressDialog::setValue(intprogress)来设置进度。

这里的进度对话框,我们必须show(),如果调用exec()的话,界面会卡住~。亲,注意show()和exec()的使用哦~~

[cpp]
view plaincopyprint?

qApp->processEvents();

qApp->processEvents();


亲,注意上面的函数。如果我把该函数屏蔽掉,会感觉界面很卡。

网友的解释是:

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

处理下事件。

比如有个事务处理比较耗时间,你可以在中间不时地processEvents()下,这样好让界面处理一下各种事件,避免看上去无反应像死掉一样。

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

九、



[cpp]
view plaincopyprint?

QPrinter printer;
QPrintDialog printDlg(&printer, this);
printDlg.setWindowTitle(tr("打印对话框"));
if(printDlg.exec() == QDialog::Accepted)
{

}

QPrinter printer;
QPrintDialog printDlg(&printer, this);
printDlg.setWindowTitle(tr("打印对话框"));
if(printDlg.exec() == QDialog::Accepted)
{

}

十、关于const 变量引用

eg:

void fun(const QString &str);

形参str是对常量对象的引用,因此可以传入临时QString 对象作为实参。然而对非常量对象的引用,不具名的对象、临时对象、和具体数值是不能够作为实参的。

在这里附上程序源代码下载链接:http://download.csdn.net/detail/fzu_dianzi/3743682

以上纯属个人学习笔记,如果哪里错了,希望提出。愿和大家一起共同学习,一起进步。我的邮箱地址是:xzy@yingzhi8.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: