//当项目复杂到一定程度时对各个组件结点进行处理
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <iostream>
using namespace std;
static QObject *PrintName(QObject *obj)
{
if(!obj)
{
return NULL;
}
qDebug()<<obj->objectName(); //输出结点名字
const QMetaObject *mobj = obj->metaObject(); //QMetaObject存放所有结点属性和方法
QString cname = mobj->className();
cout<<":"<<cname.toStdString()<<endl;
if(cname == "QPushButton")
{
QPushButton *pb = qobject_cast<QPushButton *>(obj); //对结点符合的className的QPushButton的obj转化为QPushButton类型
if(pb)
{
pb->setText(pb->text()+"class"); //设置按钮显示加入class
}
}
QObjectList cs = obj->children();
if(cs.size()<=0)
{
return NULL;
}
for(int i = 0;i < cs.size();i++) //遍历结点
{
PrintName(cs[i]);
}
}
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//打印父节点组件
QObjectList cs = this->children();
for(int i=0;i < cs.size(); i++)
{
qDebug() << cs[i]->objectName();
}
//打印子节点组件
QObjectList gs = ui->groupWidgt->children();
for(int i=0; i<gs.size(); i++)
{
qDebug() << gs[i]->objectName();
}
qDebug()<<"========================";
//打印所有结点
PrintName(this);
}
Widget::~Widget()
{
delete ui;
}