qt学习笔记(四)之对话框-Qt内建对话框
来源:互联网 发布:55开 知乎 12.26 编辑:程序博客网 时间:2024/05/18 07:32
在上一节中,我们已经清楚的了解到QDialog的子类,像QColorDialog,QFontDiog等,这些对话框我们称之为内建对话框,又叫标准对话框
这一节我们主要讲解下标准对话框的一些使用方法。
一、下图为我们要设计出来的主界面
窗口的绘制,这里就不在叙述了。
特别说明下:窗口的绘制我没有使用布局管理器,而是自己“设计”的布局。
- resize(320, 240);
首先我调用QWidget::resize(intw,inth) 来设置对话框窗口大小。
然后设计其他部件格局
- //自定义布局
- 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));
当然,我们还要对按钮进行“信号与槽”编辑
可以设计一个槽函数,然后再内部进行按钮对象的识别。
当信号被激活,应用程序会进入槽函数。我们就可以根据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返回值是未定义的)
二、颜色对话框
下面的代码是关于颜色对话框的应用
- 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);
- }
三、错误消息框
- 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();
如果我们不用exec(),而是直接show(),这时候会发现错误消息框一闪而过。
因为msgbox的生存周期在show()完就结束了,所以我们还是调用exec()。应用程序会在用户关闭消息对话框后,才return 出来。
这样讲很不恰当~~语言能力有限,哎~~只能这么解释了。
四、文件对话框
- QString FileName = QFileDialog::getOpenFileName(this, tr("打开文件"), "/home", tr("任何文件(*.c)"));
- infoTextEdit->setText(FileName);
五、字体对话框
- bool ok;
- QFont font = QFontDialog::getFont(&ok, infoTextEdit->font(), this, tr("字体对话框"));
- if(ok)
- {
- infoTextEdit->setFont(font);
- }
六、输入对话框
- bool ok;
- QString text = QInputDialog::getText(this, tr("输入对话框"), tr("输入文本"), QLineEdit::Normal, tr("fuck"), &ok);
- //判断
- if(ok && !text.isEmpty())
- {
- infoTextEdit->setText(text);
- }
七、页面设置对话框
- QPrinter printer;
- QPageSetupDialog pageDlg(&printer, this);
- pageDlg.setWindowTitle(tr("页面设置对话框"));
- if(pageDlg.exec() == QDialog::Accepted)
- {
- //下一步处理
- }
八、进度对话框
- 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);
这里的进度对话框,我们必须show(),如果调用exec()的话,界面会卡住……注意show()和exec()的使用哦~~
- qApp->processEvents();
亲,注意上面的函数。如果我把该函数屏蔽掉,会感觉界面很卡。
网友的解释是:
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
处理下事件。
比如有个事务处理比较耗时间,你可以在中间不时地processEvents()下,这样好让界面处理一下各种事件,避免看上去无反应像死掉一样。
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
九、
- QPrinter printer;
- QPrintDialog printDlg(&printer, this);
- printDlg.setWindowTitle(tr("打印对话框"));
- if(printDlg.exec() == QDialog::Accepted)
- {
- }
十、关于const 变量引用
eg:
void fun(const QString &str);
形参str是对常量对象的引用,因此可以传入临时QString 对象作为实参。然而对非常量对象的引用,不具名的对象、临时对象、和具体数值是不能够作为实参的。
- qt学习笔记(四)之对话框-Qt内建对话框
- qt学习笔记(四)之对话框-Qt内建对话框 .
- qt学习笔记(四)之对话框-Qt内建对话框
- QT学习 之 对话框 (四) 字体对话框、消息对话框、文件对话框、进程对话框
- QT学习笔记之对话框
- Qt:内建对话框
- Qt之GUI学习笔记(四)——创建对话框
- Qt学习笔记--对话框
- Qt5开发学习之Qt基本对话框(四)
- 学习笔记之Qt自定义对话框
- 《学习Qt之路2》笔记:Qt之文本对话框
- Qt内建(built-in)对话框
- Qt内建对话框简介
- qt学习笔记(三)之对话框-QDialog
- qt学习笔记(三)之对话框-QDialog
- 《学习Qt之路2》笔记:Qt对话框模式
- qt学习笔记:Qt标准对话框之QInputDialog
- QT学习笔记----登录对话框
- Unity3D学习之----------------------------------UI控件回调挂载
- nginx-图片裁剪缩放
- ios定位开发 当使用百度地图sdk没有启用object c++混编时就会出现如下错误
- LA 3401 - Colored Cubes
- python装饰器
- qt学习笔记(四)之对话框-Qt内建对话框
- ios 添加图片的三种方法
- LR性能测试结果样例分析
- npm常用命令
- 开发中的注意事项
- 3401 - Colored Cubes
- LeetCode——Count Complete Tree Nodes
- MySQL的timeout那点事
- IOS多线程