#include "mainwindow.h" #include "ui_mainwindow.h" #include #include #include #include "propitem.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { info (ui->pushButton); info (ui->treeWidget); info (this); } // #include // #include // #include void MainWindow::info (QObject * obj) { QTreeWidgetItem * top = new QTreeWidgetItem; ui->treeWidget->addTopLevelItem (top); QString s = obj->metaObject()->className(); top->setText (0, s); const QMetaObject * cls = obj->metaObject(); QTreeWidgetItem * above = top; const QMetaObject * super = cls->superClass (); while (super != NULL) { QTreeWidgetItem * node = new QTreeWidgetItem; node->setText (0, super->className()); above->addChild (node); super = super->superClass(); above = node; } QTreeWidgetItem * branch = new QTreeWidgetItem; branch->setText (0, "properties"); top->addChild (branch); int n = cls->propertyCount (); for (int i = 0; i < n; i++) { QMetaProperty p = cls->property (i); // #include "propitem.h" PropItem * node = new PropItem; node->obj = obj; node->prop = p; QVariant value = p.read (obj); QString v = value.isNull() ? "null" : value.toString(); node->setText (0, p.name () + QString (" : ") + p.typeName () + QString (" = ") + v); branch->addChild (node); } branch = new QTreeWidgetItem; branch->setText (0, "methods"); top->addChild (branch); n = cls->methodCount (); for (int i = 0; i < n; i++) { QMetaMethod m = cls->method (i); QTreeWidgetItem * node = new QTreeWidgetItem; node->setText (0, m.signature ()); branch->addChild (node); } } void MainWindow::on_pushButton_2_clicked() { QList list = ui->treeWidget->selectedItems(); foreach (QTreeWidgetItem * t , list) { PropItem * u = dynamic_cast (t); if (u != NULL) t->setForeground (0, QColor ("lime")); else t->setForeground (0, QColor ("red")); if (u != NULL) { QVariant value = ui->lineEdit->text(); u->prop.write (u->obj, value); } } }