遍历所有QObject的子节点
1.遍历子节点-获取节点对象的名称
2.QObjectList children()
3.objectName()
4.转换子节点-获取节点对象的类型
5.QMetaObject *metaObject()
6.metaObject()->className();
7.qobject_cast<QCheckBox*>(cb);
ui_widget.h
#ifndef UI_WIDGET_H
#define UI_WIDGET_H
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QLabel>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_Widget
{
public:
QPushButton *pushButton;
QPushButton *pushButton_2;
QCheckBox *checkBox;
QCheckBox *checkBox_2;
QWidget *groupwidget;
QLabel *label;
QLabel *label_2;
QPushButton *pushButton_3;
QPushButton *pushButton_4;
void setupUi(QWidget *Widget)
{
if (Widget->objectName().isEmpty())
Widget->setObjectName(QString::fromUtf8("Widget"));
Widget->resize(800, 600);
pushButton = new QPushButton(Widget);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
pushButton->setGeometry(QRect(60, 20, 89, 25));
pushButton_2 = new QPushButton(Widget);
pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));
pushButton_2->setGeometry(QRect(60, 60, 89, 25));
checkBox = new QCheckBox(Widget);
checkBox->setObjectName(QString::fromUtf8("checkBox"));
checkBox->setGeometry(QRect(60, 100, 92, 23));
checkBox_2 = new QCheckBox(Widget);
checkBox_2->setObjectName(QString::fromUtf8("checkBox_2"));
checkBox_2->setGeometry(QRect(60, 140, 92, 23));
groupwidget = new QWidget(Widget);
groupwidget->setObjectName(QString::fromUtf8("groupwidget"));
groupwidget->setGeometry(QRect(320, 120, 341, 341));
label = new QLabel(groupwidget);
label->setObjectName(QString::fromUtf8("label"));
label->setGeometry(QRect(20, 20, 67, 17));
label_2 = new QLabel(groupwidget);
label_2->setObjectName(QString::fromUtf8("label_2"));
label_2->setGeometry(QRect(20, 60, 67, 17));
pushButton_3 = new QPushButton(groupwidget);
pushButton_3->setObjectName(QString::fromUtf8("pushButton_3"));
pushButton_3->setGeometry(QRect(20, 120, 89, 25));
pushButton_4 = new QPushButton(groupwidget);
pushButton_4->setObjectName(QString::fromUtf8("pushButton_4"));
pushButton_4->setGeometry(QRect(20, 180, 89, 25));
retranslateUi(Widget);
QMetaObject::connectSlotsByName(Widget);
} // setupUi
void retranslateUi(QWidget *Widget)
{
Widget->setWindowTitle(QCoreApplication::translate("Widget", "Widget", nullptr));
pushButton->setText(QCoreApplication::translate("Widget", "PushButton", nullptr));
pushButton_2->setText(QCoreApplication::translate("Widget", "PushButton", nullptr));
checkBox->setText(QCoreApplication::translate("Widget", "CheckBox", nullptr));
checkBox_2->setText(QCoreApplication::translate("Widget", "CheckBox", nullptr));
label->setText(QCoreApplication::translate("Widget", "TextLabel", nullptr));
label_2->setText(QCoreApplication::translate("Widget", "TextLabel", nullptr));
pushButton_3->setText(QCoreApplication::translate("Widget", "PushButton", nullptr));
pushButton_4->setText(QCoreApplication::translate("Widget", "PushButton", nullptr));
} // retranslateUi
};
namespace Ui {
class Widget: public Ui_Widget {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_WIDGET_H
Widget.cpp
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->groupwidget->children();
for(int i = 0;i < gs.size(); i++)
{
qDebug()<<gs[i]->objectName();
}
}
xz@xiaqiu:~/study/csdn/day0/QObjectChildren/build$ ./testqt
"pushButton"
"pushButton_2"
"checkBox"
"checkBox_2"
"groupwidget"
"label"
"label_2"
"pushButton_3"
"pushButton_4"
xz@xiaqiu:~/study/csdn/day0/QObjectChildren/build$
遍历所有QObject的子节点
1.遍历子节点-获取节点对象的名称
2.QObjectList children()
3.objectName()
4.转换子节点-获取节点对象的类型
5.QMetaObject *metaObject()
6.metaObject()->className();
7.qobject_cast<QCheckBox*>(cb);
ui_widget.h
#ifndef UI_WIDGET_H
#define UI_WIDGET_H
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QLabel>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_Widget
{
public:
QPushButton *pushButton;
QPushButton *pushButton_2;
QCheckBox *checkBox;
QCheckBox *checkBox_2;
QWidget *groupwidget;
QLabel *label;
QLabel *label_2;
QPushButton *pushButton_3;
QPushButton *pushButton_4;
void setupUi(QWidget *Widget)
{
if (Widget->objectName().isEmpty())
Widget->setObjectName(QString::fromUtf8("Widget"));
Widget->resize(800, 600);
pushButton = new QPushButton(Widget);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
pushButton->setGeometry(QRect(60, 20, 89, 25));
pushButton_2 = new QPushButton(Widget);
pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));
pushButton_2->setGeometry(QRect(60, 60, 89, 25));
checkBox = new QCheckBox(Widget);
checkBox->setObjectName(QString::fromUtf8("checkBox"));
checkBox->setGeometry(QRect(60, 100, 92, 23));
checkBox_2 = new QCheckBox(Widget);
checkBox_2->setObjectName(QString::fromUtf8("checkBox_2"));
checkBox_2->setGeometry(QRect(60, 140, 92, 23));
groupwidget = new QWidget(Widget);
groupwidget->setObjectName(QString::fromUtf8("groupwidget"));
groupwidget->setGeometry(QRect(320, 120, 341, 341));
label = new QLabel(groupwidget);
label->setObjectName(QString::fromUtf8("label"));
label->setGeometry(QRect(20, 20, 67, 17));
label_2 = new QLabel(groupwidget);
label_2->setObjectName(QString::fromUtf8("label_2"));
label_2->setGeometry(QRect(20, 60, 67, 17));
pushButton_3 = new QPushButton(groupwidget);
pushButton_3->setObjectName(QString::fromUtf8("pushButton_3"));
pushButton_3->setGeometry(QRect(20, 120, 89, 25));
pushButton_4 = new QPushButton(groupwidget);
pushButton_4->setObjectName(QString::fromUtf8("pushButton_4"));
pushButton_4->setGeometry(QRect(20, 180, 89, 25));
retranslateUi(Widget);
QMetaObject::connectSlotsByName(Widget);
} // setupUi
void retranslateUi(QWidget *Widget)
{
Widget->setWindowTitle(QCoreApplication::translate("Widget", "Widget", nullptr));
pushButton->setText(QCoreApplication::translate("Widget", "PushButton", nullptr));
pushButton_2->setText(QCoreApplication::translate("Widget", "PushButton", nullptr));
checkBox->setText(QCoreApplication::translate("Widget", "CheckBox", nullptr));
checkBox_2->setText(QCoreApplication::translate("Widget", "CheckBox", nullptr));
label->setText(QCoreApplication::translate("Widget", "TextLabel", nullptr));
label_2->setText(QCoreApplication::translate("Widget", "TextLabel", nullptr));
pushButton_3->setText(QCoreApplication::translate("Widget", "PushButton", nullptr));
pushButton_4->setText(QCoreApplication::translate("Widget", "PushButton", nullptr));
} // retranslateUi
};
namespace Ui {
class Widget: public Ui_Widget {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_WIDGET_H
Widget.cpp
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->groupwidget->children();
for(int i = 0;i < gs.size(); i++)
{
qDebug()<<gs[i]->objectName();
}
}
xz@xiaqiu:~/study/csdn/day0/QObjectChildren/build$ ./testqt
"pushButton"
"pushButton_2"
"checkBox"
"checkBox_2"
"groupwidget"
"label"
"label_2"
"pushButton_3"
"pushButton_4"
xz@xiaqiu:~/study/csdn/day0/QObjectChildren/build$