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

  • Home
  • SEARCH
  • 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 9167805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:28:50+00:00 2026-06-17T15:28:50+00:00

I have this class: class JavaScript : public QObject { Q_OBJECT public: JavaScript(); bool

  • 0

I have this class:

class JavaScript : public QObject {    
    Q_OBJECT
    public:
        JavaScript();        
        bool executeFromFile(QString file);  
        bool enabled;

    public slots:
        void setEnabled( bool enabled );
        bool isEnabled() const;    

    private:
        QScriptEngine engine;
};

The methods are defined like this:

#include "javascript.h"

JavaScript::JavaScript() {  
    executeFromFile("test.js");
}
bool JavaScript::executeFromFile(QString file) {
    QFile scriptFile(file);
    if (!scriptFile.open(QIODevice::ReadOnly)) return false;
    QTextStream stream(&scriptFile);
    QString contents = stream.readAll();    
    scriptFile.close();
    engine.evaluate(contents, file);    
    return true;
}

void JavaScript::setEnabled( bool enabled ) {
    JavaScript::enabled = enabled;
}
bool JavaScript::isEnabled() const {
    return enabled;
}

I’m trying to access the public slots previously defined in the header file like the documentation says:

http://doc.qt.digia.com/qt/scripting.html#making-a-c-object-available-to-scripts-written-in-qtscript

The test.js file looks like this, just like the examples of the docs:

var obj = new JavaScript();
obj.setEnabled( true );
print( "obj is enabled: " + obj.isEnabled() );

But i’m not getting anything. It seems it doesn’t find the JavaScript object. What am I missing?

Doing a simple

print(1+1)

works just fine.

EDIT: An example in the qt4 webpage implements Q_PROPERTY. I tried this, but got the same result:

Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled)

EDIT 1: Tried implementing the initializer like this:

// javascript.h:

JavaScript(QObject *parent = 0);

// javascript.cpp:

JavaScript::JavaScript(QObject *parent) : QObject(parent) {}

Still nothing…

EDIT 2: Some examples inherits from QScriptable too:

class JavaScript : public QObject, public QScriptable {}

But that makes no difference either.

  • 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-06-17T15:28:51+00:00Added an answer on June 17, 2026 at 3:28 pm

    You need to create QScriptClass instead of QObject. Qt contains example of how to extend script capabilites in Qt. Take a look on Custom Script Class Example

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

Sidebar

Related Questions

I have a javascript class that I'm creating. This class has private and public
I have the following class in javascript: function User(aJid){ this.jid = aJid; this.name =
I have this class file call SMSHelper.cs First I just wanted to know is
I have this class which i would like to map: public class Contract {
i have this class public class Image { public string url { get; set;
I have this class: class View(object): def main_page(self, extra_placeholders = None): file = '/media/Shared/sites/www/subdomains/pypular/static/layout.tmpl'
I have this class: public static class CsvWriter { private static StreamWriter _writer =
I have a simple class in asp.net mvc that looks like this: public class
So I have this class: class JSObjectStorage extends \SplObjectStorage { /**Adds a JavaScript object
I have this action in my RESTful application on MVC3: [HttpPut] public void Rest(ViewModel

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.