Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Q_DECLARE_METATYPE(pointer*) and Q_PROPERTY
Qt 6.11 is out! See what's new in the release blog

Q_DECLARE_METATYPE(pointer*) and Q_PROPERTY

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 3.4k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Illogica
    wrote on last edited by Illogica
    #1

    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?

    kshegunovK 1 Reply Last reply
    0
    • I Illogica

      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?

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @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 ...?

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • I Offline
        I Offline
        Illogica
        wrote on last edited by
        #3

        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?

        kshegunovK 1 Reply Last reply
        0
        • I Illogica

          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?

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @Illogica
          Do you mean you want to do this:
          http://doc.qt.io/qt-5/qtjavascript.html#making-a-qobject-available-to-the-script-engine

          Read and abide by the Qt Code of Conduct

          I 1 Reply Last reply
          0
          • kshegunovK kshegunov

            @Illogica
            Do you mean you want to do this:
            http://doc.qt.io/qt-5/qtjavascript.html#making-a-qobject-available-to-the-script-engine

            I Offline
            I Offline
            Illogica
            wrote on last edited by
            #5

            @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!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved