Qt Creator入门笔记1

  1. 创建有窗口类:myWidget
  2. 基类有三种选择:QWidget/QMainWidget/QDialog,窗口/有菜单栏窗口/对话框

    1
    2
    3
    4
    5
    6
    7
    int main(int argc, char *argv[]) //argc命令行变量的数量,argv命令行变量数组
    {
    QApplication a(argc, argv); //在QT中应用程序对象有且仅有一个
    MainWindow w; //窗口对象
    w.show(); //显示对象
    return a.exec(); //代码阻塞到当前行
    }
  3. pro工程文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    QT       += core gui #Qt包含的模块,核心和图形,如udp和tcp在network中

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets #大于4版本的Qt包含Widget模块

    CONFIG += c++11

    SOURCES += \ #源文件
    main.cpp \
    mainwindow.cpp

    HEADERS += \ #头文件
    mainwindow.h

    FORMS += \
    mainwindow.ui
  4. 自动对齐command+i,切换同名.cpp和.h F4

  5. 按钮控件头文件#include <QPushButton>
  6. 设置按钮和窗口大小与标题

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    #include "widget.h"
    #include "ui_widget.h"
    #include <QPushButton>

    Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
    {
    ui->setupUi(this);
    QPushButton * b1 = new QPushButton;
    //b1->show(); //以顶层的方式弹出窗口控件
    b1->setParent(this); //按钮依赖于当前Widget窗口
    b1->setText("new button1");
    QPushButton * b2 = new QPushButton("new button2", this);
    b2->move(100, 100); //移动按钮,坐标
    b2->resize(1,1);//按钮重置大小
    resize(800,600); //设置窗口大小
    setFixedSize(800,600); //固定窗口大小
    setWindowTitle("the First Window"); //命名窗口标题
    }

    Widget::~Widget()
    {
    delete ui;
    }
  7. 按钮控件常用api

    1
    2
    3
    setParent(this);//设置父亲
    setText("123");设置文本
    move()

对象数模型

  1. 创建对象时,这个对象会自动添加到其父对象的children()列表
  2. 当父对象析构时,会清除掉列表中的所有对象都会被析构
  3. 当创建的对象在堆区的时候,如果指定的父亲是QObject派生下来的类,或者是QObject子类派生下来的类,可以不用管理释放操作,将对象放入对象树中

信号和槽

  1. 信号槽松散耦合,信号端和接收端没有关联,通过connect函数连接
  2. 信号函数

    1
    2
    3
    4
    5
    6
    7
    void clicked() //点击
    void pressed() //按下
    void released() //松开
    void toggled(bool checked) //状态改变
    //connect(信号发送端,信号函数地址,信号接收端,信号函数地址);
    connect(myBtn, &QPushButton::clicked, this, &QWidget::close); //点击按钮关闭窗口
    disconnect(t, teacherSingal, s, studentSlot); //断开信号
  3. 自定义信号写在signals下,返回值为void,可以有参数,只需要声明,不需要实现

  4. 触发自定义信号 emit
  5. 一个信号可以连接多个槽函数,多个信号可以连接同一个槽函数
  6. 信号和槽的参数类型类型必须一一对应,信号参数的个数可以多余槽函数的参数个数

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
109
110
111
112
113
//示例代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include "student.h"
#include "teacher.h"

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
Q_OBJECT

public:
Widget(QWidget *parent = nullptr);
~Widget();

private:
Ui::Widget *ui;

Teacher *t;
Student *s;

void classOver();
};


#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include "teacher.h"
#include "student.h"
#include <QPushButton>

Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
resize(800,600);
//创建老师对象
this->t = new Teacher(this);
//创建学生对象
this->s = new Student(this);

//函数指针指向函数地址
//为了避免信号和槽发生重载
void(Teacher:: *teacherSingal)(QString) = &Teacher::hungry;
void(Student:: *studentSlot)(QString) = &Student::treat;

connect(t, teacherSingal, s, studentSlot);
//classOver();

QPushButton * btn = new QPushButton("Class is over!", this);
//this->resize(400, 300);
connect(btn, &QPushButton::clicked, this, &Widget::classOver);

//无参数版本,示范又一个信号触发另一个信号

void(Teacher:: *teacherSingal2)(void) = &Teacher::hungry;
void(Student:: *studentSlot2)(void) = &Student::treat;

connect(t, teacherSingal2, s, studentSlot2);

QPushButton * btn2 = new QPushButton("Class is over2!", this);
btn2->move(200,0);
connect(btn2, &QPushButton::clicked, t, teacherSingal2);
disconnect(t, teacherSingal, s, studentSlot);//断开信号
//Qt4版本的信号槽连接,参数直观,但是不做类型检测
connect(t, SIGNAL(hungry()), s, SIGNAL(treat()));

//使用Lambda表达式实现点击按钮,关闭窗口
QPushButton *quit_btn=new QPushButton("quit", this);
quit_btn->move(700,0);
connect(quit_btn, &QPushButton::clicked, this, [=](){
//quit_btn->setText("退出");
this->close();
});

}

Widget::~Widget()
{
delete ui;
}


void Widget::classOver(){

emit t->hungry("水蜜桃");


}

#include "student.h"
#include <QDebug>


Student::Student(QWidget *parent) : QWidget(parent)
{


}
void Student::treat(){
qDebug()<<"请客吃饭";
}

void Student::treat(QString foodname){
qDebug()<<"请客吃饭"<<foodname.toUtf8().data();
}
WhitneyLu wechat
Contact me by scanning my public WeChat QR code
0%