华清远见上海中心22071班

对象树模型

#include <iostream>
#include <list>

using namespace std;
class obj;
typedef list<obj*> childlist;

class obj
{
public:

    childlist child;
    obj(obj* parent = nullptr)
    {
        if(parent != nullptr)
        {
            parent->child.push_back(this);
        }
    }

    virtual ~obj()
    {
        for(auto it=child.begin(); it!=child.end(); it++)
        {
            delete *it;
        }

    }

};
class A : public obj
{

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

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

    B* b = new B(&a);
    return 0;
}

信号与槽函数

.cpp

#include "mywig.h"
#include <QDebug>
#include <QPushButton>
mywig::mywig(QWidget *parent)
    : QWidget(parent)
{
    this->setWindowTitle("my first window");

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


    this->setMaximumSize(1024, 768);
    this->setMinimumSize(500, 300);

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

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

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


     //连接函数
     connect(btn1, &QPushButton::clicked, this, &mywig::pag_close);
     connect(btn3, &QPushButton::clicked, this, &mywig::set_tex);
     //connect(btn3, &QPushButton::clicked, this, &mywig::setTex);
     connect(btn3, SIGNAL(clicked()), this, SLOT(setTex()));

}

mywig::~mywig()
{
}

void mywig::pag_close()
{
    this->close();
}

void mywig::setTex()
{
    btn1->setText("close");
}

void mywig::set_tex()
{
    btn2->setText("helloworls");
}

main.cpp

#include "mywig.h"

#include <QApplication>

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

.h

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

class mywig : public QWidget
{
    Q_OBJECT

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

     QPushButton *btn1;
     QPushButton *btn2;
     QPushButton *btn3;
    void set_tex();
    void pag_close();
    void setTex();
};
#endif // MYWIG_H

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值