- 匿名函数
- 没有函数名, 只有函数体
1.1 语法
[capture](params) opt -> ret {body;};- []: 匿名函数的开始- (): 参数列表 == 和c++是一样的- opt: 可选项, 不使用可以忽略不写- mutable- throws- ->返回值: 函数的返回值, 可以省略, c++11返回值类型可以自动推导- {}: 函数体// 例子int num =10;int count = 100;QString str = "hello";[=](){printf("%d\n", num);num++;int a = 10;printf("%d\n", a);return a;}
变量的捕获方式
[]- 不捕捉[&]- 引用的方式使用外部变量[=]- 拷贝的方式使用….., 函数体内使用的是副本在匿名函数内部使用的副本, 副本默认是
const类型- 数据只能读, 不能写
[=, &foo]- foo使用引用, 其余是拷贝的方式- foo是一个代指, 指的是某一个变量的名字
[bar]- 在函数体内部使用拷贝的方式访问bar变量[this]- 在函数内部使用类的成员函数和成员变量
1.2 注意事项
lambda表达式无法修改通过复制方式捕获的外部变量
如果需要修改, 请使用引用的方式捕获外部变量
如果希望修改按值捕获的外部变量需要使用mutable
[=] mutable {return a++;}
- int a = 1, b=9;
// a是一个拷贝的副本, 默认是const类型[=] {return a++;}; // error
2. Qt中的窗口类
2.1 三个常用窗口类
QWidget
- Qt中所有窗口类的基类
- 特点: 简单
可以作为顶层窗口, 也可以作为附属窗口
- 顶层窗口: 都是有边框的, 标题栏
- 附属窗口: 另外一个窗口的子窗口
QMainWindow
- QWidget 的子类
- 有菜单栏, 工具栏, 状态栏, 浮动窗口
- 有边框和标题栏
QDialog -> 对话框窗口
- QWidget的子类
- 消息的提示
有两种显示模式
模态
- 窗口显示, 代码阻塞, 不继续向下执行
- 显示之后只能对当前窗口操作, 不能操作器父窗口
非模态
- 和模态是对应的, 相反的
2.2 QMainWindow

菜单栏: 最多有一个
- 菜单项: QAction
// 鼠标单击发出的信号[signal] void QAction::triggered(bool checked = false)
- 菜单项: QAction
工具栏: 可以有多个
- 添加控件
QAction *QToolBar::addWidget(QWidget *widget)
- 添加控件
- 主窗口中添加工具栏
void QMainWindow::addToolBar(Qt::ToolBarArea area, QToolBar *toolbar);void QMainWindow::addToolBar(QToolBar *toolbar);QToolBar *QMainWindow::addToolBar(const QString &title);
状态栏: 最多有一个

浮动窗口: 多个
2.3 QDialog
一般都是使用QDialog的子类
- QMessageBox ```c // 提示错误信息的函数 QMessageBox::StandardButton QMessageBox::critical(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton = NoButton)
// 信息提示 QMessageBox::StandardButton information(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton = NoButton);
// 提问问题 QMessageBox::StandardButton question(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = …, QMessageBox::StandardButton defaultButton = NoButton);
// 警告信息
QMessageBox::StandardButton warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton = NoButton);
// 实例代码 // 错误对话框 QMessageBox::critical(this, “Error”, “信息错误”, QMessageBox::Yes | QMessageBox::No, QMessageBox::No); // 问题对话框 QMessageBox::StandardButton ret = QMessageBox::question(this, “Question”, “有问题吗?”, QMessageBox::Yes | QMessageBox::No, QMessageBox::No); // 判断用户的选择 if(ret == QMessageBox::Yes) { QMessageBox::warning(this, “Warning”, “密码不匹配”, QMessageBox::Ok); } else { QMessageBox::information(this, “Info”, “hello, world”, QMessageBox::Ok); }
-QFileDialog- 打开窗口选择文件或目录```c[static] QString QFileDialog::getExistingDirectory(QWidget *parent, const QString &caption, const QString &dir, QFileDialog::Options options)// 返回值是选择的目录的绝对路径QString dir = QFileDialog::getExistingDirectory(this, "Open Directory","D:\\",QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks);qDebug() << dir;// 打开多个文件, 返回选择的文件的绝对路径QStringList files = QFileDialog::getOpenFileNames(this,"Select one or more files to open","D:\\","Images (*.png *.jpg)");qDebug() << files;
两种打开模式
- 模态
[virtual slot] int QDialog::exec()
- 模态
- 非模态 ```c // 设置窗口属性, 当窗口关闭的时候, 自己释放自己的内存 // Makes Qt delete this widget when the widget has accepted the close event (see QWidget::closeEvent()). // Qt::WA_DeleteOnClose 55 void QWidget::setAttribute(Qt::WidgetAttribute attribute, bool on = true);\
// 实例代码 MyWindow* dia = new MyWindow; dia->setAttribute(Qt::WA_DeleteOnClose); // 显示非模态对话框 dia->show();
<a name="6f416ae6"></a># 3. 资源文件-解决项目资源找不到的问题- 引用的资源从A拷贝到B主机, 存储资源的目录不同导致无法加载资源- 当应用程序进行编译之后, 资源文件中添加的资源会被打包到exe程序中-创建资源文件<br /><br />-配置资源文件---图片资源存放的位置<br />放到和qrc资源文件同一级目录, 或者更深的目录中, 不能放到资源文件的上级目录-资源文件的使用<br />-在程序中使用资源文件中的图片```c// 资源文件中的 路径: :前缀 图片相对路径 图片的名字// :/Image/16.icoui->actionbbb->setIcon(QIcon(":/Image/16.ico"));
4. 控件
看帮助文档的时候主要看(先看):
- 信号
- 槽函数
- 如果槽函数提供的功能不能满足需求看公共的成员函数
按钮
- QPushButton -> 最常用的一种
// 最常用信号[signal] void QAbstractButton::clicked(bool checked = false)
- QPushButton -> 最常用的一种
- QToolButton
// 最常用信号[signal] void QAbstractButton::clicked(bool checked = false)

- QRadioButton
// 不同组的按钮要放到不同 的窗口中// 常用信号[signal] void QAbstractButton::clicked(bool checked = false)
- QCheckBox ```c // 信号 [signal] void QCheckBox::stateChanged(int state) 参数:
没选中: Qt::Unchecked -> 0 选中了: Qt::Checked -> 2 一部分被选中: Qt::PartiallyChecked -> 1 -> 树状结构: 当前节点还有子节点, 需要对子节点进行选择
<br />-列表-QListWidget```cvoid QListWidget::addItem(const QString &label);void QListWidget::addItem(QListWidgetItem *item);// 一次添加多行void QListWidget::addItems(const QStringList &labels);
