QT::QObjectChildren操作
前言:
本文操作均为在vs2015+QT5.9.5版本中执行
头文件:qobjectchildren.h
#pragma once
#include <QtWidgets/QWidget>
#include "ui_qobjectchildren.h"
class QObjectChildren : public QWidget
{
Q_OBJECT
public:
QObjectChildren(QWidget *parent = Q_NULLPTR);
private:
Ui::QObjectChildrenClass ui;
};
UI文件:ui_qobjectchildren.h
/********************************************************************************
** Form generated from reading UI file 'qobjectchildren.ui'
**
** Created by: Qt User Interface Compiler version 5.9.5
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_QOBJECTCHILDREN_H
#define UI_QOBJECTCHILDREN_H
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QRadioButton>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_QObjectChildrenClass
{
public:
QRadioButton *radioButton;
QPushButton *pushButton;
QPushButton *pushButton_2;
QRadioButton *radioButton_2;
QCheckBox *checkBox;
QWidget *groupwidget;
QPushButton *pushButton_3;
QRadioButton *radioButton_3;
QCheckBox *checkBox_2;
QPushButton *pushButton_4;
QRadioButton *radioButton_4;
QWidget *widget;
QPushButton *pushButton_5;
void setupUi(QWidget *QObjectChildrenClass)
{
if (QObjectChildrenClass->objectName().isEmpty())
QObjectChildrenClass->setObjectName(QStringLiteral("QObjectChildrenClass"));
QObjectChildrenClass->resize(944, 641);
radioButton = new QRadioButton(QObjectChildrenClass);
radioButton->setObjectName(QStringLiteral("radioButton"));
radioButton->setGeometry(QRect(170, 250, 89, 16));
pushButton = new QPushButton(QObjectChildrenClass);
pushButton->setObjectName(QStringLiteral("pushButton"));
pushButton->setGeometry(QRect(170, 190, 111, 21));
pushButton_2 = new QPushButton(QObjectChildrenClass);
pushButton_2->setObjectName(QStringLiteral("pushButton_2"));
pushButton_2->setGeometry(QRect(170, 220, 111, 21));
radioButton_2 = new QRadioButton(QObjectChildrenClass);
radioButton_2->setObjectName(QStringLiteral("radioButton_2"));
radioButton_2->setGeometry(QRect(170, 280, 89, 16));
checkBox = new QCheckBox(QObjectChildrenClass);
checkBox->setObjectName(QStringLiteral("checkBox"));
checkBox->setGeometry(QRect(170, 310, 71, 16));
groupwidget = new QWidget(QObjectChildrenClass);
groupwidget->setObjectName(QStringLiteral("groupwidget"));
groupwidget->setGeometry(QRect(280, 190, 461, 281));
pushButton_3 = new QPushButton(groupwidget);
pushButton_3->setObjectName(QStringLiteral("pushButton_3"));
pushButton_3->setGeometry(QRect(30, 60, 121, 21));
radioButton_3 = new QRadioButton(groupwidget);
radioButton_3->setObjectName(QStringLiteral("radioButton_3"));
radioButton_3->setGeometry(QRect(30, 120, 89, 16));
checkBox_2 = new QCheckBox(groupwidget);
checkBox_2->setObjectName(QStringLiteral("checkBox_2"));
checkBox_2->setGeometry(QRect(30, 150, 71, 16));
pushButton_4 = new QPushButton(groupwidget);
pushButton_4->setObjectName(QStringLiteral("pushButton_4"));
pushButton_4->setGeometry(QRect(30, 30, 121, 21));
radioButton_4 = new QRadioButton(groupwidget);
radioButton_4->setObjectName(QStringLiteral("radioButton_4"));
radioButton_4->setGeometry(QRect(30, 90, 89, 16));
widget = new QWidget(groupwidget);
widget->setObjectName(QStringLiteral("widget"));
widget->setGeometry(QRect(220, 50, 171, 181));
pushButton_5 = new QPushButton(widget);
pushButton_5->setObjectName(QStringLiteral("pushButton_5"));
pushButton_5->setGeometry(QRect(10, 20, 111, 51));
retranslateUi(QObjectChildrenClass);
QMetaObject::connectSlotsByName(QObjectChildrenClass);
} // setupUi
void retranslateUi(QWidget *QObjectChildrenClass)
{
QObjectChildrenClass->setWindowTitle(QApplication::translate("QObjectChildrenClass", "QObjectChildren", Q_NULLPTR));
radioButton->setText(QApplication::translate("QObjectChildrenClass", "RadioButton", Q_NULLPTR));
pushButton->setText(QApplication::translate("QObjectChildrenClass", "Push1", Q_NULLPTR));
pushButton_2->setText(QApplication::translate("QObjectChildrenClass", "Push2", Q_NULLPTR));
radioButton_2->setText(QApplication::translate("QObjectChildrenClass", "RadioButton", Q_NULLPTR));
checkBox->setText(QApplication::translate("QObjectChildrenClass", "CheckBox", Q_NULLPTR));
pushButton_3->setText(QApplication::translate("QObjectChildrenClass", "Push4", Q_NULLPTR));
radioButton_3->setText(QApplication::translate("QObjectChildrenClass", "RadioButton", Q_NULLPTR));
checkBox_2->setText(QApplication::translate("QObjectChildrenClass", "CheckBox", Q_NULLPTR));
pushButton_4->setText(QApplication::translate("QObjectChildrenClass", "Push3", Q_NULLPTR));
radioButton_4->setText(QApplication::translate("QObjectChildrenClass", "RadioButton", Q_NULLPTR));
pushButton_5->setText(QApplication::translate("QObjectChildrenClass", "Push5", Q_NULLPTR));
} // retranslateUi
};
namespace Ui {
class QObjectChildrenClass: public Ui_QObjectChildrenClass {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_QOBJECTCHILDREN_H
主函数:main.cpp
#include "qobjectchildren.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QObjectChildren w;
w.show();
return a.exec();
}
实现文件:qobjectchildren.cpp
#include "qobjectchildren.h"
#include <QDebug>
#include <iostream>
#include <QPushButton>
using namespace std;
static QObject *PrintName(QObject *obj)
{
//判断obj是否为空
if (!obj)
{
return NULL;
}
//打印objectName()控件文本
cout << obj->objectName().toStdString();//toStdString()转换成String类型
//打印控件名称
const QMetaObject *mobj = obj->metaObject();
QString Smobj = mobj->className();
cout << ":" << Smobj.toStdString() << endl;
//判断控件名称是否为QPushButton
if (Smobj == "QPushButton")
{
//c++方式强转,由char*类型转换成QString类型
QPushButton *Qpb = qobject_cast<QPushButton*>(obj);
//判断Qpb是否为空,如果为空会自动返回NULL,如果不为空如下会修改QPushButton文本
if (Qpb)
{
Qpb->setText(Qpb->text() + "class");
}
}
//访问widget的子节点
QObjectList cs = obj->children();
//判断是否还有子节点
if (cs.size() <= 0)
{
return NULL;
}
//寻找子节点,进行遍历打印
for (int i = 0; i < cs.size(); i++)
{
PrintName(cs[i]);
}
}
QObjectChildren::QObjectChildren(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
//遍历所有节点
PrintName(this);
qDebug() << "==================================";
//遍历Widget下所有控件(不包括孙子节点)
QObjectList cs = this->children();
for (int i = 0; i < cs.size(); i++)
{
qDebug() << cs[i]->objectName();
}
qDebug()<< "==================================";
//遍历groupwidget下所有控件(不包括孙子节点)
QObjectList gs = ui.groupwidget->children();
for (int i = 0; i < gs.size(); i++)
{
qDebug() << gs[i]->objectName();
}
}