华清远见(上海中心)22071

将对象树模型手动实现一遍

#include <iostream>
#include <list>
class Obj;
using namespace std;
typedef list<Obj*> ChildList;

class Obj
{
public:
    ChildList child;
    Obj(Obj *parent=nullptr)
    {
        if(parent !=nullptr)
        {
            parent->child.push_back(this);
        }
        cout<<"Obj::构造函数"<<endl;
    }
    virtual ~Obj()
    {
        for(auto it = child.begin(); it!=child.end();it++)
        {
            delete *it;
        }
        cout<<"Obj::析构函数"<<endl;
    }
};

class A:public Obj
{
public:
    A(Obj *parent=nullptr)
    {
        if(parent!=nullptr)
        {
            parent->child.push_back(this);
        }
        cout<<"A::构造函数"<<endl;
    }
    ~A()
    {
        cout<<"A::析构函数"<<endl;
    }
};
class B:public Obj
{
public:
    B(Obj *parent=nullptr)
    {
        if(parent!=nullptr)
        {
            parent->child.push_back(this);
        }
        cout<<"B::构造函数"<<endl;
    }
    ~B()
    {
        cout<<"B::析构函数"<<endl;
    }
};

int main()
{
    A a;
    B *b=new B(&a);
    return 0;
}

 将信号与槽函数的代码手动实现一遍

头文件>>

#ifndef MYFIRST_H
#define MYFIRST_H

#include <QWidget>
#include <QPushButton>

class myfirst : public QWidget
{
    Q_OBJECT

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

public slots:
    void my_close();
    void my_text();
    void my_Text();
private:
    QPushButton *btn1;
    QPushButton *btn2;
    QPushButton *btn3;
};
#endif // MYFIRST_H

源文件>>

#include "myfirst.h"
#include <QDebug>
#include <QPushButton>

myfirst::myfirst(QWidget *parent)
    : QWidget(parent)
{
    this->setWindowTitle("my first window");

    this->setMaximumSize(1024,512);

    this->setMinimumSize(512,256);

    //this->setFixedSize(500,500);

    qDebug()<<this->windowTitle();

    btn1=new QPushButton("btn1",this);
    btn1->resize(100,50);

    btn2=new QPushButton("btn2",this);
    btn2->resize(btn1->size());
    btn2->move(btn1->width(),0);

    btn3=new QPushButton("btn3",this);
    btn3->resize(btn1->size());
    btn3->move(0,btn1->height());

    
    connect(btn1,&QPushButton::clicked,this,&myfirst::my_close);
    connect(btn3,&QPushButton::clicked,this,&myfirst::my_text);
    //connect(btn3,&QPushButton::clicked,this,&myfirst::my_Text);
    

    connect(btn3,SIGNAL(clicked()),this,SLOT(my_Text()));


}

myfirst::~myfirst()
{
}

void myfirst::my_close()
{
    this->close();
}

void myfirst::my_text()
{
    btn2->setText("world");
}

void myfirst::my_Text()
{
    btn1->setText("hello");
}

主函数文件>>

#include "myfirst.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    myfirst w;
    w.show();
    return a.exec();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值