将QT项目中,每个文件的每行代码,自行注释一遍,重新手动实现对象树模型

 工程文件

QT       += core gui
#添加类库默认gui,如果需要添加其他类库在后面补上
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
#添加widgets类库,在qt4.0以后的版本需要添加,之前的版本默认在gui类库中
CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
#警告
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
#源文件
SOURCES += \
    main.cpp \
    widget.cpp
#头文件
HEADERS += \
    widget.h
#图形化界面文件
FORMS += \
    widget.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

头文件 

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QToolButton>//根据自己需要添加所需要组件的头文件
//引入头文件
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }//对Ui命名空间已定义的Widget声明
QT_END_NAMESPACE

class Widget : public QWidget //自己定义的Widget图形化类,公有继承已经定义好的QWidget模板类
{
    Q_OBJECT//信号与槽对应的元

public:
    Widget(QWidget *parent = nullptr);//构造函数声明,参数为父组件的指针

    ~Widget();//析构函数声明,函数体在源文件中;

private:
    Ui::Widget *ui;//通过ui这个指针,对ui可视界面拖拽的组件操作
    QToolButton *btn1;//自定义组件
};
#endif // WIDGET_H

源文件 

#include "widget.h"
#include "ui_widget.h"
//自定类的构造函数函数体
Widget::Widget(QWidget *parent) //自定义组件的定义
    : QWidget(parent)     //Widget继承QWiget,这里是用父类的有参构造初始化成员
    , ui(new Ui::Widget) //ui指针实例化空间
{
    ui->setupUi(this); //调用ui类里面的成员函数
   this->btn1=new QToolButton(this); //自定义组件实例化空间

    this->btn1->setText("按钮1");//用指向自定义组件的this指针更改自定义组件内容

    ui->btn2->setText("按钮2");//更改ui界面上的组件,使用ui指针

}

Widget::~Widget()
{
    delete ui;//将ui指针空间释放
}

 

主函数

#include "widget.h"        //引入工程中的头文件
#include <QApplication>      //引入应用程序的头文件
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);    //实例化一个应用程序类对象

    Widget w;               //在栈区,实例化对象
    w.show();               //调用展示函数


    return a.exec();             //轮询等待处理
}

 

 

 对象树模型

#include <iostream>
#include <list>//引入链表模板的头文件
using namespace std;
class Myobject
{
 public:
    //创建子组件链表
    list<Myobject *> child;

    Myobject(Myobject * parent=nullptr)//构造函数,当没给定父组件时,让参数指向空
    {
        if(parent!=nullptr)
        {
           //将自己的地址尾插到父组件储存子组件地址的链表中
            parent->child.push_back(this);
        }
          cout<<"fu构造函数"<<endl;
    }
    virtual ~Myobject()//父类为虚构函数,子类的析构函数也为虚析构
    {
        
        for(auto p=child.begin();p!=child.end();p++)//从链表开头到结尾释放当前组件的子组件空间
        {
            delete  *p;
            cout<<"fu析构函数"<<endl;
        }

    }
};
//派生类
class A:public Myobject
{
public:
    A(Myobject *parent=nullptr)
    {
      if(parent!=nullptr)
       {
          parent->child.push_back(this);
       }
      cout<<"A::构造函数"<<endl;
    }
    virtual ~A()
      {
          cout<<"A::析构函数"<<endl;
      }
};
class B:public Myobject
{
public:
   B(Myobject *parent=nullptr)
    {
      if(parent!=nullptr)
       {
          parent->child.push_back(this);
       }
      cout<<"B::构造函数"<<endl;
    }
   virtual ~B()
     {
         cout<<"B::析构函数"<<endl;
     }
};
int main()
{
    A w;
    B *btn=new B(&w);
//    B *btn=new B;
//    delete btn;
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值