Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6232241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:05:00+00:00 2026-05-24T10:05:00+00:00

Problem I am making a project using Q_OBJECT and Q_PROPERTY to access some objects

  • 0

Problem

I am making a project using Q_OBJECT and Q_PROPERTY to access some objects from scripts. I have two problems:

  1. making classes that use forward declarations scriptable
  2. returning a property as pointer

Explanations

1. Why forward declaration?

The class B gets the forward declaration to A, because A needs the complete B type in the header due to the templates. B needs only an incomplete type (A*) in the header, thus the forward declaration is valid.

2. Why returning a pointer?

We cannot return a copy, as we need access to the actual object in the script. We cannot return a reference, as Qt does not allow slots to return references – their type would be ignored, they would only return void*.

Code

Complete code download on pastebin or as ZIP archive or as ZIP archive of minimal example is available, for testing / playing: I needed to split up the files for the forward declaration and for MOC. I added a Makefile to test it. Make deps: g++, moc, Qt.

Important parts

class A; // forward declaration necessary, see explanation above

class B : public QObject {
    Q_OBJECT
    Q_PROPERTY(A a READ GetA) // <-- ERROR HERE
    // ...

public slots:
    A* GetA() { 
        return mA; 
    }

private:
    A* mA;
    // ...
}

The error line in the script:

print(bObj.GetA().GetName());

Compile error

This error disappears when I comment out the Q_PROPERTY above.

tmp/B.moc.hpp:95:51: error: invalid use of incomplete type ‘struct A’
tmp/../B.hpp:10:7: error: forward declaration of ‘struct A’

Script exception

When leaving out the Q_PROPERTY and calling the GetA() method as a slot from the script, I get the following exception:

Line 7: "TypeError: cannot call GetA(): unknown return type `A*' 
        (register the type with qScriptRegisterMetaType())"

When registering A* with qRegisterMetaType<A*>("A*"); this changes to:

Line 7: "TypeError: Result of expression 'bObj.GetA().GetName' 
        [undefined] is not a function." 

That shows that GetA() does not return the A object, or somehow it returns a pointer, but the script cannot dereference it. GetA() then actually returns a QVariant(A*), can this be used somehow?

Questions:

  1. Can I somehow make a Q_PROPERTY from an incomplete type, or how could I avoid the forward declaration?
  2. Can I return a reference in a slot (maybe some tricks, e.g. a class that wraps the pointer and "overrides" the script operator., if something similar exists) or
  3. Can I somehow dereference a QVariant(A*) to A in QtScript?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:05:01+00:00Added an answer on May 24, 2026 at 10:05 am
    1. Your property type is A, not A*, that’s why you get very reasonable error.
    2. You should use QScriptValue. Look this code. It works Ok:

      class A; // forward declaration necessary, see explanation above
      
      class B : public QObject
      {
          Q_OBJECT
      
          // Using QScriptValue, made from A instead of A to allow script work correctly with an object
          Q_PROPERTY(QScriptValue a READ GetA) 
      
      public slots:
          QScriptValue GetA() {
              //making QScriptValue from A. Type conversion in C style only due to limitation of incomplete type
              //In real app it's beter to put defenition of this slot after A's defenition
              return static_cast<QScriptEngine*>(parent())->newQObject((QObject*)mA);
          }
      
      private:
          A* mA;
          // ...
      public:
          //I decided my object will be a child of scriptEngine, but you can take a pointer to it in some other way
          B(QScriptEngine * parent);
      };
      
      class A: public QObject
      {
          Q_OBJECT
      public slots:
          QString GetName() const {return "a name";}
      public:
          A(QScriptEngine*parent):QObject(parent){}
      
      };
      
      B::B(QScriptEngine *parent):QObject(parent), mA(new A(parent)){}
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a project using GAE, and have a terrible problem. I wanted to
I have a project where I am using Java RMI to make objects remotely
My only problem is making them line up three-across and have equal spacing. Apparently,
I'm having some difficulties with the following problem: I'm making a little game where
I am making an application with Java Swing and i have a problem. I
currently im making some crawler script, one of problem is sometimes if i open
im making some script with mechanize.browser module. one of problem is all other thing
I have a very big problem. I am making a CRM (Costumer Relationship Management)
I'm making a new Project which is a class library. my problem is I
I'm using git for a personal project at the moment and have run into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.