Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Q_DECLARE_METATYPE(pointer*) and Q_PROPERTY



  • I have this class (chopped down for clarity):

    namespace server{
        class clientinfo
        {
            Q_GADGET
            Q_PROPERTY(int clientnum MEMBER clientnum)  // not working
            Q_PROPERTY(QString name READ _name WRITE _setName) // not working
    
        public slots:
            QString _name();
            void _setName(const QString &s);
    
        public:
            clientinfo();
            ~clientinfo();
    
            int clientnum;
            string name;
        };
    }
    Q_DECLARE_METATYPE(server::clientinfo*)
    

    I must declare server::clientinfo as a pointer since it's part of a bigger non-qt project that passes pointers here and there. How would I implement my two Q_PROPERTYs to access the members of my class?

    EDIT:
    I pass this pointer to my class to QJSEngine:

    QJSValueList capsule;
    capsule << qserver->js.toScriptValue<clientinfo *>(ci);
    

    Calling the properties from javascript will not work, since it's a pointer, not the whole class.
    How would I use the pointer within javascript?


  • Qt Champions 2017

    @Illogica said:

    I must declare server::clientinfo as a pointer since it's part of a bigger non-qt project that passes pointers here and there. How would I implement my two Q_PROPERTYs to access the members of my class?

    I don't understand the question. Also what exactly isn't working with the properties, the in-code comments aren't saying much ...?



  • Yes, sorry. I edited the question since it lacked a little of context...
    I'm passing my custom class to QJSEngine. I need to be able to change the class properties from within a js script.
    If I pass the regular object (not a pointer) the properties work, but when I send back the object to c++, the rest of the program (which is quite big) will sooner or later segfault since the object I'm returning is not the original one.
    So, I want to pass a pointer to my object to js, but how do i access the properties of the object since I'm only passing a pointer?


  • Qt Champions 2017



  • @kshegunov yes, I was too in a rush to solve the problem and had the totally wrong approach.
    I needed to pass pointers to objects to QJSEngine, and then the javascript environment is supposed to call c++ functions passing this pointer again.
    So the right approach is via QJSValue QJSEngine::newQObject(QObject *object), which wraps the QObject pointer, and return it via QObject *QJSValue::toQObject().
    Anyway, thanks for caring!


Log in to reply