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 6802983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:17:19+00:00 2026-05-26T19:17:19+00:00

I am trying to write a sample code for qt script. I thought I

  • 0

I am trying to write a sample code for qt script. I thought I am doing the right thing when I declare the QObjecy with the copy construtor and I also took the liberty to declare the = operator. But this code keeps giving me the

'QObject::QObject' : cannot access private member declared in class 'QObject'

Error.

I am declaring a MyClass which is a QObject as follows. I am aware of the fact that this can some one see what I am doing a wroing here.

The header:

#ifndef SCRIPT_CLASSES_H
#define SCRIPT_CLASSES_H
#include "QObject"
#include "QtScript/QScriptValue"
#include "QtScript/QScriptable"
#include "QtScript/QScriptClass"

class MyClass : public QObject
{
    Q_OBJECT
//    Q_PROPERTY( int _id WRITE setId READ id )
public :
    MyClass(QObject *aparent =0) ;
    ~MyClass();

//     bool operator =(MyClass obj);

public slots:
    void setId(int d);
    int id() const ;
//    bool MyClass::equals(const MyClass &other);
private :
    int _id;
};

class QScriptEngine;
class Script_Classes : public QObject, public QScriptClass
{
public:
    Script_Classes(QScriptEngine *engine);
    ~Script_Classes();
private :
    static QScriptValue myClassToScript(QScriptEngine *engine,const MyClass &in);
    static void myClassFromScript(const QScriptValue &object, MyClass &out);
};

#endif // SCRIPT_CLASSES_H

And my source class is as follows:

#include "script_classes.h"
#include "QMetaType"
#include "QtScript/QScriptEngine"
#include "QtScript/QScriptValue"

Q_DECLARE_METATYPE(MyClass)
Q_DECLARE_METATYPE(MyClass*)

MyClass::MyClass(QObject *aparent) : QObject (aparent){}

MyClass::~MyClass(){}

void MyClass::setId(int d){
    _id = d;
}


int MyClass::id() const{
   return _id;
}

bool MyClass::equals(const MyClass &other)
{
    return id() == other.id();
}

bool MyClass::operator =(MyClass obj){
    return id()==obj.id();
}



Script_Classes::Script_Classes(QScriptEngine *engine):QObject(engine),QScriptClass(engine)
{
    qScriptRegisterMetaType<MyClass>(engine, myClassToScript, myClassFromScript);
    MyClass testClass(this);
}

void Script_Classes::myClassFromScript(const QScriptValue &object, MyClass &out){
out.setId(object.property("id").toInt32());
}

QScriptValue Script_Classes::myClassToScript(QScriptEngine *engine, const  MyClass &in)    
{
    QScriptValue value = engine->newObject();
    value.setProperty("id", in.id());
    return value;
}
  • 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-26T19:17:20+00:00Added an answer on May 26, 2026 at 7:17 pm

    The problem is that you cannot copy a QObject. From the QObject documentation:

    QObject has neither a copy constructor nor an assignment operator.
    This is by design. Actually, they are declared, but in a private
    section with the macro Q_DISABLE_COPY(). In fact, all Qt classes
    derived from QObject (direct or indirect) use this macro to declare
    their copy constructor and assignment operator to be private. The
    reasoning is found in the discussion on Identity vs Value on the Qt
    Object Model page.

    The main consequence is that you should use pointers to QObject (or to
    your QObject subclass) where you might otherwise be tempted to use
    your QObject subclass as a value.
    For example, without a copy
    constructor, you can’t use a subclass of QObject as the value to be
    stored in one of the container classes. You must store pointers.

    Also QObject has not the == implemented so you cannot compare two instance of your class.

    PS What is the point of overloading the = operator and making it operate like the == one? This makes your code obfuscated and debugging much more complex.

    EDIT

    Assume you have the following simple class inheriting from QObject

    class A : public QObject
    {
         Q_OBJECT
    public:
         int anInt;
         double aDouble;
    }
    

    Assume now that you want to create two inctances of A somewhere in your code.

    A a1;
    A a2;
    

    It is illegal to call a1=a2 since QObject's = operator is not public. What you need to do in order to achieve the copying of the data is to do it manually :

    a2.anInt = a1.anInt
    a2.aDouble = a1.aDouble
    

    On the other hand if you used pointers it is totally legal to point at the same object

    A* a1 = new A;
    A* a2 = new A;
    a1 = a2;
    

    Now both a1 and a2 point at the same memory location and have the same data. If you want to have two different objects you could create a constructor with argument a pointer to the object. For our simple class you could have:

    A::A(A* a)
    {
       anInt = a->anInt;
       aDouble = a->aDouble;
    }
    

    and now it is legal do:

    A a1;
    A a2(&a1);
    

    If you wonder why QObject does not allow assignment read the Identity vs Value part of the Object Model documentation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to write a simple C# script to copy file to directory one level
I am trying to write some simple code which will read a text file
I'm trying to write a simple code in Java to connect to a memcache
I trying to write a simple function to call unmanaged code from managed code.
I am trying to write a simple tool using Shoes. This will indent code
Hi I'm trying to write a piece of code for a simple verification method
I am trying to write a sample Raw socket program to clear my understanding
I'm trying to write something similar to this (sorry if the sample is not
I'm trying to write a simple PHP script which automatically sets up new etherpads
I am trying to write a simple bash script. It just puts out a

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.