QT学习笔记(一)

创建项目

1、选择Application->Qt Widget Application创建项目
在这里插入图片描述

QPushbutton

1、在主mainwindow.cpp文件中引入QPushButton,并在构建函数中创建按钮,创建connect函数使此按钮点击能够关闭主窗口,具体实现方法如下

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QPushButton>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QPushButton * btn = new QPushButton;//创建按钮
    btn->setParent(this);//给按钮设置setParent属性,使其作用在当前作用域下(在窗口上方显示)
    btn->setText("QT第一个按钮");//给按钮设置文字
    QPushButton * btn2 = new QPushButton("QT第二个按钮",this);//第二个按钮
    resize(800,600);//设置主窗口大小
    btn2->move(100,100);//设置按钮坐标,起始坐标(0,0)位于窗口左上角,(X,Y)中X表示横坐标,Y表示纵坐标,
    setWindowTitle("QT的第一个窗口标题"); //设置主窗口标题
    connect(mybutton, &mypushbutton::clicked, this, &MainWindow::close);
}

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

验证QT内存回收机制

1、在Sources下创建 MyButton类,在定义类选项中选择其父类未QWidget(QPushButton的父类是QAbstractButton,而QAbstractButton的父类是QWidget)
在这里插入图片描述

在这里插入图片描述
2、在mybutton.h中引入QPushbutton,类定义为QPushbutton类,public中声明析构函数

#ifndef MYBUTTON_H
#define MYBUTTON_H
#include<QPushButton>
#include <QWidget>

class MyButton : public QPushButton
{
    Q_OBJECT
public:
    explicit MyButton(QWidget *parent = nullptr);
    ~MyButton();
signals:

public slots:
};  

#endif // MYBUTTON_H

3、在mybutton.cpp文件中实现析构函数与构造函数,并将其类对象改为QPushbutton类

#include "mybutton.h"
#include <QDebug>
MyButton::MyButton(QWidget *parent) : QPushButton(parent)
{
    qDebug()<< "按钮已创建";
}

MyButton::~MyButton(){
    qDebug()<< "按钮内存已释放";
}

4、在主窗口中创建自定义按钮mybutton,然后运行,关闭窗口程序,控制台打印如下图,按钮会随着主程序的关闭而释放内存,效果如下。
在这里插入图片描述

//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QPushButton>
#include"mybutton.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QPushButton * btn = new QPushButton;//创建按钮
    btn->setParent(this);//给按钮设置setParent属性,使其作用在当前作用域下(在窗口上方显示)
    btn->setText("QT第一个按钮");//给按钮设置文字
    btn->setFixedSize(100,50);//设置按钮大小,(X,Y),X
    QPushButton * btn2 = new QPushButton("QT第二个按钮",this);//第二个按钮
    resize(800,600);//设置主窗口大小
    btn2->move(100,100);//设置按钮坐标,起始坐标(0,0)位于窗口左上角,(X,Y)中X表示横坐标,Y表示纵坐标,
    setWindowTitle("QT的第一个窗口标题"); //设置主窗口标题
    MyButton * my = new MyButton; //创建自定义按钮
    my->move(100,300);
    my->setText("我自己的按钮");
    my->move(300,0);
    my->setParent(this);
    connect(btn, &QPushButton::clicked, this, &MainWindow::close);
}

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

信号和槽

之前在QPushButton创建的时候有用到connect()函数,该函数的第一个参数是信号发送者,第二个参数是发送的信号,第三个参数是信号的接收者,第四个参数是处理的槽函数。

自定义的信号和槽

1、在资源文件夹中新建teacher与student类,两者都继承QObject类。在student.h文件,在public或public slots中声明槽函数treat()(在QT5.5以下版本只可在public slots中声明)。在student.cpp文件中实现treat()函数。同样的在teacher.h文件中的signals中声明hungry()函数(信号)。

//student.h
#ifndef STUDENT_H
#define STUDENT_H

#include <QObject>

class student : public QObject
{
    Q_OBJECT
public:
    explicit student(QObject *parent = nullptr);

signals:

public slots:
    void treat();
};

#endif // STUDENT_H
//teacher.h
#ifndef TEACHER_H
#define TEACHER_H

#include <QObject>

class teacher : public QObject
{
    Q_OBJECT
public:
    explicit teacher(QObject *parent = nullptr);

signals:
    void hungry();

public slots:
};

#endif // TEACHER_H
//student.cpp
#include "student.h"
#include <QDebug>
student::student(QObject *parent) : QObject(parent)
{

}
void student::treat(){
    qDebug()<< "请老师吃饭";
}
//teacher.cpp
#include "teacher.h"
#include<QDebug>
teacher::teacher(QObject *parent) : QObject(parent)
{

}

2、在主窗口头文件的私有变量中声明student与teacher指针和overclass()函数。MainWindow.cpp文件下测试

//MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "teacher.h"
#include "student.h"
#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    student *st;
    teacher *te;
    void overclass();
};

#endif // MAINWINDOW_H

//MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QPushButton>
#include"mybutton.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QPushButton * btn = new QPushButton;//创建按钮
    btn->setParent(this);//给按钮设置setParent属性,使其作用在当前作用域下(在窗口上方显示)
    btn->setText("QT第一个按钮");//给按钮设置文字
    btn->setFixedSize(100,50);//设置按钮大小,(X,Y),X
    QPushButton * btn2 = new QPushButton("QT第二个按钮",this);//第二个按钮
    resize(800,600);//设置主窗口大小
    btn2->move(100,100);//设置按钮坐标,起始坐标(0,0)位于窗口左上角,(X,Y)中X表示横坐标,Y表示纵坐标,
    setWindowTitle("QT的第一个窗口标题"); //设置主窗口标题
    MyButton * my = new MyButton; //创建自定义按钮
    my->move(100,300);
    my->setText("我自己的按钮");
    my->move(300,0);
    my->setParent(this);
    this->st = new student;//在当前域下创建学生对象
    this->te = new teacher;//在当前域下创建老师对象
    connect(btn, &QPushButton::clicked, this, &MainWindow::close);
    connect(te,&teacher::hungry,st,student::treat);
    overclass();
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::overclass(){
    emit te->hungry();
}

测试结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值