1.对象树模型
#include <iostream>
#include<list>
class obj;
using namespace std;
typedef list<obj*> childlist;
class obj
{
public:
childlist childen;
public:
obj(obj* parent=nullptr) {
if(parent!=nullptr)
{
parent->childen.push_back(this);
}
}
virtual ~obj() {
for(auto it=childen.begin();it!=childen.end();it++)
{
delete *it;
}
}
};
//定义子类
class A:public obj
{
public:
A(obj *parent=nullptr ) {
if(parent!=nullptr)
{
parent->childen.push_back(this);
}
cout<<"A::construct"<<endl;
}
~A() {
cout<<"A::xigou"<<endl;
}
};
class B:public obj
{
public:
B(obj *parent=nullptr ) {
if(parent!=nullptr)
{
parent->childen.push_back(this);
}
cout<<"B::construct"<<endl;
}
~B() {
cout<<"B::xigou"<<endl;
}
};
int main()
{
A a;
B *b=new B(&a);
return 0;
}
2.信号与槽函数
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include<QPushButton>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void page_quit();
void change_2();
void change2();
private:
QPushButton *btn1;
QPushButton *btn2;
QPushButton *btn3;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include<QDebug>
#include<QPushButton>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
this->setWindowTitle("first window");
//di第一种使用方式,类似cout
qDebug()<<this->windowTitle();
//第二种使用,类似printf
qDebug("%d\t%d\n",520,1314);
//设置窗口大小
this->setMaximumSize(1024,768);//最大窗口
this->setMinimumSize(500,300);//最小窗口
this->setFixedSize(800,600);//固定大小
// this->move(30,30);//移动窗口位置
//this->setWindowFlag(Qt::FramelessWindowHint);//窗口洁净
//this->setWindowOpacity(0.9);//设置窗口透明度
btn1=new QPushButton("quit",this);
btn1->resize(100,50);
btn2=new QPushButton(this);
btn2->resize(btn1->size());
btn2->move(100,0);
btn3=new QPushButton("click",this);
btn3->resize(btn1->size());
btn3->move(0,50);
//qt5版本
connect(btn1,&QPushButton::clicked,this,&MainWindow::page_quit);
connect(btn3,&QPushButton::clicked,this,&MainWindow::change_2);
connect(btn3,&QPushButton::clicked,this,&MainWindow::change2);
}
MainWindow::~MainWindow()
{
}
void MainWindow::page_quit()
{
this->close();
}
void MainWindow::change_2()
{
btn2->setText("hello world");
}
void MainWindow::change2()
{
btn1->setText("close");
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}