Skip to content

QtWS: Super Early Bird Tickets Available!

  • 0 Votes
    2 Posts
    181 Views
    R

    One more example:

    #include <QCoreApplication> #include <iostream> #include <QMetaObject> #include "example.h" #include <QDebug> #include <QMetaProperty> #include <QPointer> void gadgetExplorer(void *ptr, const char* s) { //Receives Player and access its meta-object int typeId = QMetaType::type(s); const QMetaObject *mo = QMetaType::metaObjectForType(typeId); qInfo() << "Q_GADGET: " << mo->className(); QMetaProperty mp; for(int i = 1; i < mo->propertyCount(); i++) { mp = mo->property(i); qInfo() << "Q_PROPERTY: " << mp.typeName() << "Value: " << mp.readOnGadget(ptr); if(int(mp.type()) == 1024) gadgetExplorer(ptr, mp.typeName()); } } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); qRegisterMetaType<Player>(); qRegisterMetaType<P2D>(); qRegisterMetaType<P2D*>(); Player p1; p1.setSpeed(30); p1.p2d = new P2D(); p1.p2d->setX(10.0); p1.p2d->setY(25.0); gadgetExplorer(&p1, "Player"); return a.exec(); }

    gadgetExplorer() works fine for the first Q_GADGET, but for the second one the read property doesn't work. Is it possible to access the second's Q_GADGET pointer?

  • 0 Votes
    3 Posts
    320 Views
    JKSHJ

    @raphasauer said in Is it possible to read a Q_PROPERTY with a QMetaObject pointer only?:

    I only have a QMetaObject of the gadget.

    To add to @kshegunov's post: All instances of your QGadget share the same QMetaObject. You can check this yourself:

    Player p1; Player p2; qDebug() << "Are they the same?" << (&p1.staticMetaObject == &p2.staticMetaObject)

    See also:

    https://doc.qt.io/qt-5/qobject.html#staticMetaObject-var (this is written for QObjects but it also applies to QGadgets) https://doc.qt.io/qt-5/qobject.html#Q_GADGET
  • 0 Votes
    4 Posts
    471 Views
    R

    Thanks @VRonin and @KroMignon for your insights!

  • 0 Votes
    3 Posts
    507 Views
    R

    @VRonin Solved! Thanks for your time!