#include <iostream>
#include <list>
using namespace std;
class objtree
{
private:
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;
}
virtual ~A()
{
cout<<"A::析构函数";
}
};
class B:public objtree
{
public:
B(objtree *parent = nullptr)
{
if(parent != nullptr)
{
parent->child().push_back(this);
}
cout<<"B::构造函数"<<endl;
}
virtual ~B()
{
cout<<"B::析构函数"<<endl;
}
};
int main()
{
B b;
A *a = new A(&b);
return 0;
}
.pro
QT += core gui
QT += 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 \
zuoye1.cpp
HEADERS += \
zuoye1.h
FORMS += \
zuoye1.ui
# 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 ZUOYE1_H
#define ZUOYE1_H
#include <QWidget>
#include <QPushButton>
#include <QTextToSpeech>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class zuoye1; }
QT_END_NAMESPACE
class zuoye1 : public QWidget
{
Q_OBJECT
signals:
void signal_1(); //声明信号函数
public slots:
void show1(); //声明槽函数
public:
zuoye1(QWidget *parent = nullptr);
~zuoye1();
private:
Ui::zuoye1 *ui;
QPushButton btn1; //定义第一个按钮
QPushButton btn2; //定义第二个按钮
QPushButton btn3; //定义第三个按钮
QTextToSpeech speech; //定义一个播报者
};
#endif // ZUOYE1_H
.cpp
#include "zuoye1.h"
#include "ui_zuoye1.h"
#include<QDebug>
void zuoye1::show1()
{
this->close();
}
zuoye1::zuoye1(QWidget *parent)
: QWidget(parent)
, ui(new Ui::zuoye1)
{
ui->setupUi(this);
this->setWindowTitle("第一个窗口");
btn1.setParent(this); //设置父控件
btn1.resize(75,30); //设置大小
btn1.move(0,this->height()/2); //设置位置
//qDebug()<<btn1.width();
btn1.setText("可用");
btn2.setParent(this); //设置父控件
btn2.resize(75,30); //设置大小
btn2.move(btn1.width(),this->height()/2); //设置位置
btn2.setText("播报");
btn3.setParent(this); //设置父控件
btn3.resize(75,30); //设置大小
btn3.move(2*(btn1.width()),this->height()/2); //设置位置
btn3.setText("关闭");
connect(&btn1, &QPushButton::clicked, [&](){
emit this->signal_1();
});
connect(this, &zuoye1::signal_1, [&](){
btn2.setEnabled(true);
});
connect(&btn2, &QPushButton::clicked, [&](){
speech.say(btn3.text());
btn2.setEnabled(false);
});
connect(&btn3, &QPushButton::clicked, this, &zuoye1::show1);
}
zuoye1::~zuoye1()
{
delete ui;
}