1.
#include <iostream>
#include <list>
using namespace std;
class ObjTree
{
list<ObjTree *> childlist; //存放子对象的指针链表
public:
ObjTree(ObjTree *parent =nullptr){
if(parent!=nullptr)
{
parent->childlist.push_back(this);
}
}
virtual~ObjTree(){
for(auto it=childlist.begin();it!=childlist.end();++it)
{
delete *it;
}
}
list<ObjTree *> &child()
{
return childlist;
}
};
//定义子类
class A:public ObjTree
{
public:
A(ObjTree *parent =nullptr){
if(parent!=nullptr)
{
parent->child().push_back(this);
}
cout<<"A的构造函数"<<endl;
}
~A(){
cout<<"A的析构函数"<<endl;
}
};
class B:public ObjTree
{
public:
B(ObjTree *parent =nullptr){
if(parent!=nullptr)
{
parent->child().push_back(this);
}
cout<<"B的构造函数"<<endl;
}
~B(){
cout<<"B的析构函数"<<endl;
}
};
int main()
{
B b; //实例化b控件
A *a = new A(&b); //此时A依附与B
return 0;
}
2
.pro
QT += core gui texttospeech
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
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 \
homework.cpp
HEADERS += \
homework.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
.h
#ifndef HOMEWORK_H
#define HOMEWORK_H
#include <QMainWindow>
#include <QPushButton>
#include <QTextToSpeech>
class homework : public QMainWindow
{
Q_OBJECT
signals:
void singal_1();
public slots:
void showMsg();
void say_mes();
public:
homework(QWidget *parent = nullptr);
~homework();
//
QPushButton *btn1;
QPushButton *btn2;
QPushButton *btn3;
//
QTextToSpeech speech;
};
#endif // HOMEWORK_H
.cpp
#include "homework.h"
#include "ui_homework.h"
void homework::showMsg()
{
}
void homework::say_mes()
{
speech.say("啊哈哈哈哈,鸡汤来喽");
homework::btn2->setEnabled(false);
}
homework::homework(QWidget *parent)
: QMainWindow(parent)
{
this->resize(1000,800);
this->setMaximumSize(1500,1000);
this->setMinimumSize(500,200);
this->setFixedSize(1000,800);
//设置窗口标题
this->setWindowTitle("故事会");
//设置窗口位置
this->move(100,100);
/*********************设置按钮大小,位置***********************/
btn1 = new QPushButton(this);
//btnl->setParent(this); //设置父空间
btn1->resize(100,40);
btn1->move(0,height()/2);
btn1->setText("复用");
btn2 = new QPushButton(this);
btn2->move(btn1->width()+50,height()/2);
btn2->resize(btn1->size());
btn2->setText("开讲");
btn3 = new QPushButton("关闭",this);
btn3->resize(btn1->size());
btn3->move(btn1->width()+btn2->width()+100,height()/2);
/*********************设置按钮大小,位置***********************/
//让'开讲' 按钮讲故事,并使按钮使能关闭
connect(btn2,&QPushButton::clicked,this,&homework::say_mes);
//开启'开讲'
connect(btn1,&QPushButton::clicked,[=](){
btn2->setEnabled(true);
});
//关闭窗口
connect(btn3,&QPushButton::clicked,[=](){
this->close();
});
}
homework::~homework()
{
}
main
#include "homework.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
homework w;
w.show();
return a.exec();
}