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

来源:互联网 发布:55开 知乎 12.26 编辑:程序博客网 时间:2024/05/18 07:32

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


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

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


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

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

[cpp] view plaincopy
  1. resize(320, 240);     

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

然后设计其他部件格局

[cpp] view plaincopy
  1. //自定义布局  
  2. infoTextEdit->setGeometry(QRect(20, 130, 280, 100));  
  3. colorBtn->setGeometry(QRect(20 , 10, 80, 30));  
  4. errorBtn->setGeometry(QRect(120 , 10, 80, 30));  
  5. fileBtn->setGeometry(QRect(220 , 10, 80, 30));  
  6. fontBtn->setGeometry(QRect(20 , 50, 80, 30));  
  7. inputBtn->setGeometry(QRect(120 , 50, 80, 30));  
  8. pageBtn->setGeometry(QRect(220 , 50, 80, 30));  
  9. progressBtn->setGeometry(QRect(20 , 90, 80, 30));  
  10. printBtn->setGeometry(QRect(120 , 90, 80, 30));  
主要是调用了QWidget::setGeometry(intax,intay,intaw,intah

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

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

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

[cpp] view plaincopy
  1. 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 plaincopy
  1. QPalette palette = infoTextEdit->palette(); //获取文本编辑的调色板对象  
  2. //QColor color = QColorDialog::getColor(QPalette::Base, this);  
  3. //颜色对话框的初始颜色值为文本编辑的背景色  
  4. QColor color = QColorDialog::getColor(palette.color(QPalette::Base), this);  
  5. //如果用户在颜色对话框点击取消的话,得到的color是无效的  
  6. if(color.isValid())  
  7. {  
  8.     //QPalette::Base 通常用于背景色  
  9.     palette.setColor(QPalette::Base, color);  
  10.     infoTextEdit->setPalette(palette);  
  11. }  
这里我们用上了调色板,调色板可以用来设置窗口部件、按钮窗口部件、文本输入窗口部件的背景色和前景色,通过ColorRole来指定,在Qt的文档中都有说明。

三、错误消息框

[cpp] view plaincopy
  1. QErrorMessage msgbox(this);  
  2.   
  3. //这里主要是为了区别错误消息框中的”再次显示消息“  
  4. msgbox.setWindowTitle(tr("错误消息框"));  
  5. msgbox.showMessage(tr("错误消息框1"));  
  6. msgbox.showMessage(tr("错误消息框1"));  
  7. msgbox.showMessage(tr("错误消息框1"));  
  8. msgbox.showMessage(tr("错误消息框2"));  
  9. msgbox.showMessage(tr("错误消息框3"));  
  10. msgbox.exec();  
我们可以创建一个QErrorMessage对象,然后分别设置显示内容,最后让该对话框执行。

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

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

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

四、文件对话框


[cpp] view plaincopy
  1. QString FileName = QFileDialog::getOpenFileName(this, tr("打开文件"), "/home", tr("任何文件(*.c)"));  
  2.   
  3. infoTextEdit->setText(FileName);  
文件对话框的创建比较简单.QFileDialog::getOpenFileName()第三个参数为设定初始默认路径,第四个参数是过滤器。可以这么说:过滤器指定的文件格式才有效。

五、字体对话框

[cpp] view plaincopy
  1. bool ok;  
  2. QFont font = QFontDialog::getFont(&ok, infoTextEdit->font(), this, tr("字体对话框"));  
  3. if(ok)  
  4. {  
  5.     infoTextEdit->setFont(font);  
  6. }  
当我们选择好要设置的字体格式,点击OK后,bool ok的值为true。

六、输入对话框


[cpp] view plaincopy
  1. bool ok;  
  2. QString text = QInputDialog::getText(this, tr("输入对话框"), tr("输入文本"), QLineEdit::Normal, tr("fuck"), &ok);  
  3.   
  4. //判断  
  5. if(ok && !text.isEmpty())  
  6. {  
  7.     infoTextEdit->setText(text);  
  8. }  
输入对话框就不介绍了,我们也可以定义自己的输入对话框类。

七、页面设置对话框


[cpp] view plaincopy
  1. QPrinter printer;  
  2. QPageSetupDialog pageDlg(&printer, this);  
  3. pageDlg.setWindowTitle(tr("页面设置对话框"));  
  4. if(pageDlg.exec() == QDialog::Accepted)  
  5. {  
  6.     //下一步处理  
  7. }  
我们都可以查看Qt的帮助文档,来完成这些程序设计。

八、进度对话框

[cpp] view plaincopy
  1. QProgressDialog progressDlg(tr("正在复制文件"), tr("取消"), 0, 10000, this);  
  2. progressDlg.setWindowTitle(tr("进度条对话框"));  
  3. //设置为模态对话框  
  4. progressDlg.setWindowModality(Qt::WindowModal);  
  5. //如果这里使用exec 进度对话框会卡主  
  6. progressDlg.show();  
  7. for(int i = 0; i < 10000; i++)  
  8. {  
  9.     progressDlg.setValue(i);  
  10.     qApp->processEvents();  
  11.   
  12.     if(progressDlg.wasCanceled())  
  13.     {  
  14.         break;  
  15.     }  
  16. }  
  17. progressDlg.setValue(10000);  
我们可以通过调用QProgressDialog::setValue(intprogress)来设置进度。

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

[cpp] view plaincopy
  1. qApp->processEvents();  

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

网友的解释是:

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

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
九、

[cpp] view plaincopy
  1. QPrinter printer;  
  2. QPrintDialog printDlg(&printer, this);  
  3. printDlg.setWindowTitle(tr("打印对话框"));  
  4. if(printDlg.exec() == QDialog::Accepted)  
  5. {  
  6.   
  7. }  

十、关于const 变量引用

eg:

void fun(const QString &str);

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

0 0
原创粉丝点击