2.关于QT中的Dialog(模态窗口),文件选择器,颜色选择器,字体选择器,消息提示窗口
1 新建一个空项目
A 编写 .pro文件
QT += gui widgets HEADERS += \ MyDialog.h SOURCES += \ MyDialog.cpp |
B 编写MyDialog.h
#ifndef MYDIALOG_H #define MYDIALOG_H #include <QDialog> class MyDialog:public QDialog { Q_OBJECT public: explicit MyDialog(QWidget *parent = 0); QString _strDir; void paintEvent(QPaintEvent *); signals: public slots: void slotButtonClick(); }; #endif // MYDIALOG_H |
C 编写:MyDialog.cpp
#include "MyDialog.h" #include <QPushButton> #include <QDebug> #include <QFileDialog> #include <QFileInfo> #include <QColorDialog> #include <QFontDialog> #include <QMessageBox> #include <QPainter> #include <QApplication> MyDialog::MyDialog(QWidget *parent) : QDialog(parent) { QPushButton* button = new QPushButton("Click me",this); connect(button, SIGNAL(clicked()), this, SLOT(slotButtonClick())); } void MyDialog::slotButtonClick() { #if 0 QDialog* dlg = new QDialog; int ret; QPushButton* button = new QPushButton(dlg); connect(button, SIGNAL(clicked()), dlg, SLOT(reject())); /* * 在模态对话框中,exec有自己的消息循环,并且把app的消息循环接管了 * 如果Dialog是通过exec来显示,那么可以通过accepted或者rejected来关闭 * 窗口,如果Dialog是通过show来显示,那么可以通过close来关闭窗口, * 这个和QWidget一样的 * * 有许多特殊的dailog:文件选择,MessageBox,颜色选择,字体选择,打印预览,打印 */ ret = dlg->exec(); if(ret == QDialog::Accepted) { qDebug() << "accepted"; } if(ret == QDialog::Rejected) { qDebug() << "rejected"; } //上面的运行结果如下: #endif #if 0 //通过下面的方式打开保存文件 QString strFilename = QFileDialog::getSaveFileName( NULL, "Select file for save", _strDir, "pic file (*.png *.jpg)"); //运行结果: #endif #if 0 //打开一个文件 #endif #if 0 //选择一个存在的文件夹 QString strFilename = QFileDialog::getExistingDirectory(); if(strFilename.isEmpty()) { qDebug() << "select none"; return; } qDebug() << strFilename; QFileInfo fileInfo(strFilename); _strDir = fileInfo.filePath(); #endif #if 0 //颜色选择框 QColorDialog color; color.exec(); QColor c = color.selectedColor(); #endif #if 0 //字体选择器 QFontDialog fontDialog; fontDialog.exec(); QFont font = fontDialog.selectedFont(); #endif #if 0 //MessageBox,消息提示窗口 int ret = QMessageBox::question(this, "????", "realy do .......", QMessageBox::Yes| QMessageBox::No| QMessageBox::YesAll| QMessageBox::NoAll); if(ret == QMessageBox::Yes) { qDebug() << "user select yes"; } if(ret == QMessageBox::No) { qDebug() << "user select no"; } #endif } void MyDialog::paintEvent(QPaintEvent *) { QPainter p(this); p.drawLine(QLine(0,0,200,200)); } int main(int argc,char* argv[]) { QApplication app(argc,argv); MyDialog dlg; dlg.show(); return app.exec(); } |
2.关于QT中的Dialog(模态窗口),文件选择器,颜色选择器,字体选择器,消息提示窗口相关推荐
- MFC PreTranslateMessage(MSG* pMsg)中调用DoModal()模态窗口问题
在PreTranslateMessage(MSG* pMsg)中调用DoModal()模态窗口如下: 1 BOOL CMainDlg::PreTranslateMessage(MSG* pMsg) ...
- Qt中为工程添加资源文件、给按钮添加图片
Qt中为工程添加资源文件.给按钮添加图片 1.使用QIcon类.代码如下: QIcon icon;icon.addFile(tr("res/icon/wall.png"));ui- ...
- qt中制作添加 .ts 翻译文件
qt中制作添加 .ts 翻译文件 QT 中可以用其自身的工具对界面语言进行翻译,以实现支持语言的效果,这对于开发用户跨越语言区域的工具来说是一个特别实用的功能. 下面介绍怎么制作和使用qt的翻译文件: ...
- QT中如何在主窗口中添加子窗口
1.方法 原理其实很简单,和在窗口上动态(代码的形式)添加控件的方法一样,但需要设置一下子窗口的属性: 在子窗口构造函数中添加代码: setWindowFlags(Qt::FramelessWindo ...
- Qt中Widget Aplication项目的文件内容介绍
项目创建完成之后会出现如下界面: 之后对项目中的各个文件内容进行介绍 1 001.pro文件.一般项目中的.pro 文件是项目的项目文件 QT += core gui #Qt包含的模块greaterT ...
- JS中ShowModalDialog(模态窗口)详细使用
基本介绍: showModalDialog() (IE 4+ 支持) showModelessDialog() (IE 5+ 支持) ...
- Qt中正确设置子窗口关闭方式
在做Qt界面设计时,通过一个按钮打开子窗口,即以new方式创建子窗口,再点击按钮后,调用close关闭子窗口,但子窗口程序还在运行,说明子窗口资源未能得到及时释放.解决的办法是: CDA_Plot = ...
- Qt中Qlabel控件实现文本多种颜色样式显示,支持换行。
在Qt文本开发中,需要用到文本不通颜色显示样式,有的开发者可能会选择一种颜色一个Qlabel控件,这样是可以实现相应的效果,但是会出现文本长度不确定,导致换行问题. 下面介绍一种在同一个Qlabel控 ...
- QT中打开网址或者html文件
使用命令:QDesktopServices :: openUrl(QUrl(QLatin1String("网址或者文件的绝对地址"); 需要包含的头文件有: #include< ...
最新文章
- 网站优化工作关键——稳定排名!
- 【洛谷 P2763】 试题库问题(最大流)
- boost::make_iterator_range用法的测试程序
- vscode中PyLint报错Unable to import解决方案
- bspline怎么使用 python_资源分享:微软官方的Python入门教程
- mysql遇到时区问题的坑(Java解决方案)
- 外卖小哥高考623分!查分后淡定送外卖
- linq与数据库之添加
- C++ STL list容器中元素的存取和访问
- 从 1.9 到 1.11,聊聊 PyFlink 的核心功能演进(附 Demo 代码)
- 如何快速搭建在线教育知识付费系统(附带源码)
- XenApp and XenDesktop 7.15 LTSR CU6发布
- 备战面试日记(4.2.13)- (框架.Spring【十三】之Spring IOC 源码finishBeanFactoryInitialization())
- HMI设计RGB配色表
- linux配置maven环境变量
- 跟着团子学SAP PS:项目计划成本与项目预算设计思路
- 腾讯课堂老师可以看到学生本人吗?
- detach分片表非常慢的一个案例
- 微型博客网站Twitter打假
- python爬取图片失败显示404_python3.7中Web抓取时出现http404错误
热门文章
- Python中的高阶函数map
- Python多任务(1.多任务的介绍、并发和并行概念及小例子)
- 情感分析基于词典(算例代码)
- OpenCASCADE绘制测试线束:图形命令之VIS 查看器命令
- wxWidgets:wxTaskBarIcon 演示
- wxWidgets:wxSystemOptions类用法
- wxWidgets:wxRecursionGuard类用法
- boost::phoenix::if_else相关的测试程序
- boost::multi_array模块实现index_base 修改工具的测试
- boost::filesystem模块Microsoft TCHAR 的使用示例的测试程序