#include"widget.h"#include"ui_widget.h"#include<qpushbutton>#include<mypushbutton.h>#include<QtDebug>Widget::Widget(QWidget *parent):QWidget(parent),ui(new Ui::Widget){
ui->setupUi(this);//按钮生成方法一
QPushButton * btn1=new QPushButton;//Sets the parent of the widget to parent, and resets the window flags.//The widget is moved to position (0, 0) in its new parent.
btn1->setParent(this);//This property holds the text shown on the button
btn1->setText("Button");//按钮生成方法二
QPushButton * btn2=newQPushButton("Second",this);//窗口大小由控件大小决定
btn2->move(100,100);resize(600,400);//设置窗口标题setWindowTitle("firset windos");//固定窗口大小setFixedSize(600,400);//新按键
MyPushButton *myBtn=new MyPushButton;
myBtn->setText("close");
myBtn->setParent(this);
myBtn->move(200,200);//信号与处理(槽)connect(myBtn,&MyPushButton::clicked,this,&Widget::close);//发送者是对象本身connect(myBtn,&QPushButton::clicked,this,&Widget::close);//发送者是父类//自定义信号与槽,1.定义对象 2.创建链接 3.调用触发函数this->zt=newTeacher(this);//创建老师对象this->st=newStudent(this);void(Teacher::*teachersignal1)(void)=&Teacher::hungry;void(Student::*Studentslot1)(void)=&Student::treat;connect(zt,teachersignal1,st,Studentslot1);Singal_incur();//槽函数中发生重载void(Teacher::*teachersignal2)(QString)=&Teacher::hungry;void(Student::*Studentslot2)(QString)=&Student::treat;connect(zt,teachersignal2,st,Studentslot2);//用按钮触发信号
QPushButton * btn3=newQPushButton("Class begin",this);//窗口大小由控件大小决定
btn2->move(300,300);this->resize(600,400);connect(btn3,&QPushButton::clicked,this,&Widget::Singal_incur);//断开信号disconnect(zt,teachersignal2,st,Studentslot2);//信号可以连接多个槽函数,有ros发布消息的味道,信号和槽函数双向多映射//信号对应的槽函数必须类型一一对应//信号和槽函数的参数,应该前者大于后者//lamda表达式[=](){
btn3->setText("aaaa");}();}voidWidget::Singal_incur()//信号发送{//emit zt->hungry();
emit zt->hungry("egg");}Widget::~Widget(){delete ui;qDebug()<<"widget的析构调用";//对象树从上之下构造,从下至上析构,但打印输出的结果却相反}