• 匿名函数
  • 没有函数名, 只有函数体

1.1 语法

  1. [capture](params) opt -> ret {body;};
  2. - []: 匿名函数的开始
  3. - (): 参数列表 == 和c++是一样的
  4. - opt: 可选项, 不使用可以忽略不写
  5. - mutable
  6. - throws
  7. - ->返回值: 函数的返回值, 可以省略, c++11返回值类型可以自动推导
  8. - {}: 函数体
  9. // 例子
  10. int num =10;
  11. int count = 100;
  12. QString str = "hello";
  13. [=](){
  14. printf("%d\n", num);
  15. num++;
  16. int a = 10;
  17. printf("%d\n", a);
  18. return a;
  19. }
  • 变量的捕获方式

    • []- 不捕捉
    • [&]- 引用的方式使用外部变量
    • [=]- 拷贝的方式使用….., 函数体内使用的是副本

      • 在匿名函数内部使用的副本, 副本默认是const类型

        • 数据只能读, 不能写
    • [=, &foo] - foo使用引用, 其余是拷贝的方式

      • foo是一个代指, 指的是某一个变量的名字
    • [bar] - 在函数体内部使用拷贝的方式访问bar变量
    • [this] - 在函数内部使用类的成员函数和成员变量

1.2 注意事项

  • lambda表达式无法修改通过复制方式捕获的外部变量

  • 如果需要修改, 请使用引用的方式捕获外部变量

  • 如果希望修改按值捕获的外部变量需要使用mutable

    1. [=] mutable {return a++;}
  • int a = 1, b=9;
    1. // a是一个拷贝的副本, 默认是const类型
    2. [=] {return a++;}; // error

2. Qt中的窗口类

2.1 三个常用窗口类

  • QWidget

    • Qt中所有窗口类的基类
    • 特点: 简单
    • 可以作为顶层窗口, 也可以作为附属窗口

      • 顶层窗口: 都是有边框的, 标题栏
      • 附属窗口: 另外一个窗口的子窗口
  • QMainWindow

    • QWidget 的子类
    • 有菜单栏, 工具栏, 状态栏, 浮动窗口
    • 有边框和标题栏
  • QDialog -> 对话框窗口

    • QWidget的子类
    • 消息的提示
    • 有两种显示模式

      • 模态

        • 窗口显示, 代码阻塞, 不继续向下执行
        • 显示之后只能对当前窗口操作, 不能操作器父窗口
      • 非模态

        • 和模态是对应的, 相反的

2.2 QMainWindow

1. Lambda表达式 - 图1

  • 菜单栏: 最多有一个

    • 菜单项: QAction
      1. // 鼠标单击发出的信号
      2. [signal] void QAction::triggered(bool checked = false)
  • 工具栏: 可以有多个

    • 添加控件
      1. QAction *QToolBar::addWidget(QWidget *widget)
  • 主窗口中添加工具栏
    1. void QMainWindow::addToolBar(Qt::ToolBarArea area, QToolBar *toolbar);
    2. void QMainWindow::addToolBar(QToolBar *toolbar);
    3. QToolBar *QMainWindow::addToolBar(const QString &title);
  • 状态栏: 最多有一个
    1. Lambda表达式 - 图2

  • 浮动窗口: 多个

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); }

  1. -
  2. QFileDialog
  3. - 打开窗口选择文件或目录
  4. ```c
  5. [static] QString QFileDialog::getExistingDirectory(QWidget *parent, const QString &caption, const QString &dir, QFileDialog::Options options)
  6. // 返回值是选择的目录的绝对路径
  7. QString dir = QFileDialog::getExistingDirectory(this, "Open Directory",
  8. "D:\\",
  9. QFileDialog::ShowDirsOnly
  10. | QFileDialog::DontResolveSymlinks);
  11. qDebug() << dir;
  12. // 打开多个文件, 返回选择的文件的绝对路径
  13. QStringList files = QFileDialog::getOpenFileNames(
  14. this,
  15. "Select one or more files to open",
  16. "D:\\",
  17. "Images (*.png *.jpg)");
  18. qDebug() << files;
  • 两种打开模式

    • 模态
      1. [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();

  1. <a name="6f416ae6"></a>
  2. # 3. 资源文件
  3. -
  4. 解决项目资源找不到的问题
  5. - 引用的资源从A拷贝到B主机, 存储资源的目录不同导致无法加载资源
  6. - 当应用程序进行编译之后, 资源文件中添加的资源会被打包到exe程序中
  7. -
  8. 创建资源文件
  9. <br />![](assets/1563089352780.png#alt=1563089352780)
  10. <br />![](assets/1563089385454.png#alt=1563089385454)
  11. -
  12. 配置资源文件
  13. -
  14. ![](assets/1563089457602.png#alt=1563089457602)
  15. -
  16. ![](assets/1563089516837.png#alt=1563089516837)
  17. -
  18. 图片资源存放的位置<br />
  19. 放到和qrc资源文件同一级目录, 或者更深的目录中, 不能放到资源文件的上级目录
  20. -
  21. 资源文件的使用
  22. <br />![](assets/1563089871682.png#alt=1563089871682)
  23. -
  24. 在程序中使用资源文件中的图片
  25. ```c
  26. // 资源文件中的 路径: :前缀 图片相对路径 图片的名字
  27. // :/Image/16.ico
  28. ui->actionbbb->setIcon(QIcon(":/Image/16.ico"));

4. 控件

  • 看帮助文档的时候主要看(先看):

    • 信号
    • 槽函数
    • 如果槽函数提供的功能不能满足需求看公共的成员函数
  • 按钮

    • QPushButton -> 最常用的一种
      1. // 最常用信号
      2. [signal] void QAbstractButton::clicked(bool checked = false)
  • QToolButton
    1. // 最常用信号
    2. [signal] void QAbstractButton::clicked(bool checked = false)


1. Lambda表达式 - 图3
1. Lambda表达式 - 图4

  • QRadioButton
    1. // 不同组的按钮要放到不同 的窗口中
    2. // 常用信号
    3. [signal] void QAbstractButton::clicked(bool checked = false)
  • QCheckBox ```c // 信号 [signal] void QCheckBox::stateChanged(int state) 参数:

没选中: Qt::Unchecked -> 0 选中了: Qt::Checked -> 2 一部分被选中: Qt::PartiallyChecked -> 1 -> 树状结构: 当前节点还有子节点, 需要对子节点进行选择

  1. <br />![](assets/1563095264058.png#alt=1563095264058)
  2. -
  3. 列表
  4. -
  5. QListWidget
  6. ```c
  7. void QListWidget::addItem(const QString &label);
  8. void QListWidget::addItem(QListWidgetItem *item);
  9. // 一次添加多行
  10. void QListWidget::addItems(const QStringList &labels);

5. 窗口布局