Qt中的lambda表达式
跳转界面前先学习一个知识点是lambda表达式。
Lambda我们可以将其理解为一个未命名的内联函数。
与任何函数类似,一个lambda具有一个返回类型,一个参数列表和一个函数体。但与函数不同,lambda可能定义在函数内部。
一个lambda表达式具有如下形式:
[capture list] (parameter list) ->return type {function body}
capture list: 捕获列表,是一个lambda所在函数中定义的局部变量列表(通常为空)
parameter list:参数列表
return type:返回类型
function body:函数体
但是与普通函数不同,lambda必须使用尾置返回来指定返回类型
我们可以忽略参数列表和返回类型,但必须永远包含捕获列表和函数体
1 2 3 4 5 6
| this->setGeometry(800, 480); QPushButton *pushButton = new QPushButton("开关", this); pushButton -> setGeometry( 200, 100, 40 ,20); connect( pushButton, SIGNAL(clicked()), this, [=](){ this->close(); })
|
结果就是可以点击开关就可以关闭界面。
设计界面(画图)
添加资源文件

添加一个界面的ui图片。

之后还是添加一个文件,这个文件是作为一个下一个界面的文件。要勾选QMainWindow作为基类。
进行绘图
在mypage.cpp中编写界面大小。
1 2 3 4 5
| #include "mypage.h" mypage::mypage(QWidget *parent) : QMainWindow(parent) { this->setFixedSize(1080, 900); }
|
在mainwindow.h中添加绘图的函数,因为在MainWindow的构造函数只运行一次。
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
| #ifndef MAINWINDOW_H #define MAINWINDOW_H
#include <QMainWindow> #include <QPainter> #include <QPixmap> #include <QPaintEvent> #include <QPushButton>
QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE
class MainWindow : public QMainWindow { Q_OBJECT
public: void paintEvent(QPaintEvent *); MainWindow(QWidget *parent = nullptr); ~MainWindow();
private: Ui::MainWindow *ui; QPushButton *pushButton; }; #endif
|
mainwindow.cpp文件中
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
| #include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> #include <QPixmap> #include <QPainter> #include <QPushButton> #include "mypage.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); this->setFixedSize( 800, 480 ); QPushButton *pushButton = new QPushButton("jump",this);
pushButton->setGeometry(100, 200, 40, 30); mypage *scene = new mypage; connect(pushButton, &QPushButton::clicked, this,[=](){ this->close(); scene->show(); }); } MainWindow::~MainWindow() { delete ui; } void MainWindow::paintEvent(QPaintEvent *) { QPainter painter(this); QPixmap pixmap(":/ig.png"); painter.drawPixmap( 0, 0, this->width(), this->height(), pixmap); }
|
点击完界面的按钮就能进入下一个界面。
从新界面点击按钮在返回主界面。
在mypage.h文件中加入信号,这个信号是为了在次界面点击按钮时候来激发信号。
1 2
| signals: void returnpage();
|
修改mypage.cpp文件,加入按钮,并连接槽函数。
1 2 3 4 5 6 7 8 9 10
| #include "mypage.h" mypage::mypage(QWidget *parent) : QMainWindow(parent) { this->setFixedSize(800, 480); pushButton1 = new QPushButton("返回", this); pushButton1 -> setGeometry( 400, 300, 40 ,30); connect(pushButton1, &QPushButton::clicked, this, [=](){ emit chooseBack(); }); }
|
返回修改mainwindow.cpp文件
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
| #include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> #include <QPixmap> #include <QPainter> #include <QPushButton> #include "mypage.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); this->setFixedSize( 800, 480 ); QPushButton *pushButton = new QPushButton("jump",this);
pushButton->setGeometry(100, 200, 40, 30); mypage *scene = new mypage; connect(pushButton, &QPushButton::clicked, this,[=](){ this->close(); scene->show(); }); connect(scene, &mypage::chooseBack, this,[=](){ scene->hide(); this->show(); }); } MainWindow::~MainWindow() { delete ui; } void MainWindow::paintEvent(QPaintEvent *) { QPainter painter(this); QPixmap pixmap(":/ig.png"); painter.drawPixmap( 0, 0, this->width(), this->height(), pixmap); }
|

