Qt Creator入门2

Lambda表达式 {}

  1. Lambda表达式用于创建匿名函数,[]标识一个lambda的开始,不能省略
  • []为空,表示没有任何参数
  • [=]函数体内可以使用Lambda所在作用范围内所有可见的局部变量,并且是值传递
  • [&]引用传递
  • [a]按a值传递
  • [&a]按a的引用传递
  1. ()标识重载,没有参数时可以省略,参数传递方式与[]一致
  2. mutable修饰符,可以修改按值传递的拷贝,[m]()mutable{m=1+1, qDebug()<<m; };
  3. 函数返回值类型[]()->int{return 1;}
  4. {}函数体
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//QMainWindow实例代码

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMenuBar>
#include <QToolBar>
#include <QDebug>
#include <QStatusBar>
#include <QPushButton>
#include <QLabel>
#include <QDockWidget>
#include <QTextEdit>
#include <QDialog>
#include <QAction>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
resize(800,600);

//添加菜单栏只能添加一个
QMenuBar *bar = menuBar();
setMenuBar(bar);


//新建菜单
QMenu *fileMenu=bar->addMenu("文件");
//QMenu *editMenu=bar->addMenu("编辑");

//创建菜单项
fileMenu->addAction("新建");
fileMenu->addSeparator(); //添加分割线
fileMenu->addAction("打开");

//工具栏 可以多个
QToolBar * toolbar = new QToolBar(this);
//默认停靠在左侧
addToolBar(Qt::LeftToolBarArea, toolbar);
//设置可以左右停靠
toolbar->setAllowedAreas(Qt::LeftToolBarArea | Qt::RightToolBarArea);

//设置浮动
toolbar->setFloatable(false);
//设置移动
toolbar->setMovable(false);


//状态栏最多一个set
QStatusBar *stBar = statusBar();
setStatusBar(stBar);

QLabel *label = new QLabel("提示信息", this);
stBar->addWidget(label);

//铆接部件,可以有多个add
QDockWidget * dockWidget = new QDockWidget("浮动", this);
addDockWidget(Qt::BottomDockWidgetArea, dockWidget); //默认底部
dockWidget->setAllowedAreas(Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea);

//设置中心部件
QTextEdit * edit = new QTextEdit(this);
setCentralWidget(edit);


//对话框
QPushButton *btn = new QPushButton("按钮", this);
connect(btn, &QPushButton::clicked, this, [=](){
//非模态对话框可以在打开后对其他窗口进行操作,反之不行
QDialog dlg(this);
dlg.exec(); //阻塞的状态
dlg.setAttribute(Qt::WA_DeleteOnClose); //关闭时释放窗口
qDebug()<<"弹出";
});

//系统标准对话框

QMessageBox::critical(this, "critical", "错误");


//信息对话框
QMessageBox::information(this, "information", "信息");

//提问对话框
//参数1 父亲 参数2 title 参数3 显式内容 参数4 关联的按键类型 参数5 默认关联回车按键
if(QMessageBox::question(this, "question", "询问", QMessageBox::Save|QMessageBox::Cancel)){
qDebug()<<"选择的是保存";
//做一些操作
}
else
qDebug()<<"选择是取消";


//QMessageBox::question(this, "question", "询问", QMessageBox::Save|QMessageBox::Cancel);


//警告对话框
QMessageBox::warning(this, "wairing", "警告");


}

MainWindow::~MainWindow()
{
delete ui;
}
WhitneyLu wechat
Contact me by scanning my public WeChat QR code
0%