[[xml2019]]
 
#include "mainwindow.h"
#include "ui_mainwindow.h"
 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
 
void MainWindow::put (QString s)
{
    ui->textEdit->append(s);
}
 
void MainWindow::clear ()
{
    ui->textEdit_2->setText("");
}
 
void MainWindow::info (QString s)
{
    ui->textEdit_2->append(s);
}
 
#include <QMetaProperty>
 
void MainWindow::on_pushButton_clicked()
{
    QObject * obj = ui->pushButton;
    // obj = ui->textEdit;
    const QMetaObject * cls = obj->metaObject();
    put (QString ("Type name : ") + cls->className());
    put ("");
 
    int cnt = cls->propertyCount();
    for (int inx = 0 ; inx < cnt; inx++)
    {
        QMetaProperty prop = cls->property(inx);
        QString n = prop.name ();
        QVariant v = prop.read (obj);
        QString t = v.typeName();
        put (n + " : " + t + " = " + v.toString());
    }
 
    int k = cls->indexOfProperty("text");
    if (k >= 0)
    {
       QMetaProperty prop = cls->property(k);
       prop.write (obj, "abc");
    }
 
    cnt = cls->methodCount();
    for (int inx = 0 ; inx < cnt; inx++)
    {
        QMetaMethod meth = cls->method(inx);
        put ("method " + meth.name());
 
    }
 
    k = cls->indexOfMethod("setVisible(bool)");
    if (k >= 0)
    {
        QMetaMethod meth = cls->method(k);
        QGenericReturnArgument result;
        meth.invoke (obj, result, Q_ARG (bool, false));
    }
 
    /*
    QPushButton * b = dynamic_cast < QPushButton * > (obj);
    if (b != nullptr)
        b->setText ("abc");
    */
 
}
 
#include <QXmlStreamWriter>
 
void MainWindow::on_pushButton_2_clicked()
{
    QString output = "";
    QXmlStreamWriter w (&output);
    w.setAutoFormatting(true);
    w.writeStartDocument();
    w.writeStartElement("window");
 
    w.writeStartElement("button");
    QObject * obj = ui->pushButton;
    const QMetaObject * cls = obj->metaObject();
    w.writeAttribute ("type", cls->className());
 
    QStringList names;
    names << "objectName" << "text" << "x" << "y";
 
    int cnt = cls->propertyCount();
    for (int inx = 0 ; inx < cnt; inx++)
    {
        QMetaProperty prop = cls->property(inx);
        QString n = prop.name ();
        QVariant v = prop.read (obj);
        QString t = v.typeName();
 
        if (names.contains(n))
        {
            w.writeStartElement (n);
            w.writeAttribute ("type", t);
            w.writeCharacters (v.toString());
            w.writeEndElement ();
        }
    }
 
    w.writeEndElement(); // end of button
 
    w.writeEndElement();
    w.writeEndElement();
    w.writeEndDocument();
    ui->textEdit->setText (output);
}
 
#include <QXmlStreamReader>
 
void MainWindow::on_pushButton_3_clicked()
{
    QString input = ui->textEdit->toPlainText();
    QXmlStreamReader r (input);
 
    QObject * obj = ui->pushButton;
    const QMetaObject * cls = obj->metaObject();
 
    clear ();
 
    bool b = false;
    QString n = "";
    while (!r.atEnd())
    {
      r.readNext();
      if (b && r.tokenType() == r.StartElement)
      {
          n = r.name ().toString();
          info ("property name " + n );
      }
      if (b && n != "" && r.tokenType() == r.Characters)
      {
          QString v = r.text ().toString();
          v = v.simplified();
          if (v != "")
          {
              int inx = cls->indexOfProperty (n.toLatin1());
              if (inx >= 0)
              {
                 QMetaProperty prop = cls->property (inx);
                 info ("xml property " + n + " = " + v);
                 if (prop.type() == QVariant::Int)
                     prop.write (obj, v.toInt());
                 else
                     prop.write (obj, v);
                 info ("object property " + n + " = " + prop.read (obj).toString ());
              }
          }
      }
      if (r.tokenType() == r.StartElement && r.name() == "button")
      {
          b = true;
      }
      if (r.tokenType() == r.EndElement && r.name() == "button")
      {
          b = false;
      }
    }
 
}
 
xml2019.txt · Last modified: 2019/11/12 15:57 by 147.32.6.116
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki