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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:38:52+00:00 2026-06-11T07:38:52+00:00

I tried declaring a signal in a prototype and then connecting it is script

  • 0

I tried declaring a signal in a prototype and then connecting it is script funcition for some reason it does not work as I hoped. My code is as follows. Could some one help me in this.

What I expected was, once I called p.setText('New String') in the script code, since setText emits the textChanged signal it should invoke the slot which is catchSignal(text) already connected in the script code.

Prototype header

#ifndef SCRIPTACTION_H
#define SCRIPTACTION_H

#include <QObject>
#include <QtScript>

class ScriptAction : public QObject , public QScriptable
{
    Q_OBJECT

 public:
    ScriptAction(QObject *parent = 0);

 signals:
    void textChanged(const QString changedString);

};

#endif // SCRIPTACTION_H

Class

#include "scriptaction.h"
#include <QAction>

Q_DECLARE_METATYPE(QAction*)


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

Main Class

#include <QApplication>
#include <QDebug>
#include <QAction>
#include "scriptaction.h"
#include <QPushButton>

Q_DECLARE_METATYPE(QAction*)

QScriptValue qAction_Constructor(QScriptContext *ctx, QScriptEngine *eng)
{
    qDebug() << "QAction is called";
    if(ctx->isCalledAsConstructor())
    {
        QObject *parent = ctx->argument(0).toQObject();
        QAction *action = new QAction("Test",parent);
        return eng->newQObject(action, QScriptEngine::ScriptOwnership);
    } else {
        return QString("invalid call. Use new Constructor");
    }
}

int main(int argc, char *argv[])
{
    QApplication app(argc,argv);
    QScriptEngine engine;
    //Evaluating a simaple expresssion
    qDebug() << engine.evaluate("1+2").toNumber();
    QPushButton button;
    QScriptValue buttonScript= engine.newQObject(&button);
    engine.globalObject().setProperty("button", buttonScript);
    engine.evaluate("button.text ='Hello Text'; button.show()");

    //QAction Prototype
    ScriptAction qsAction ;
    QScriptValue script_proto = engine.newQObject(&qsAction);
    engine.setDefaultPrototype(qMetaTypeId<QAction*>(), script_proto);
    QScriptValue ctor = engine.newFunction(qAction_Constructor , script_proto);
    QScriptValue metaObject = engine.newQMetaObject(&QObject::staticMetaObject, ctor);
    engine.globalObject().setProperty("QSAction" , metaObject);
    engine.evaluate("var p = new QSAction(button);p.textChanged.connect(catchSignal);");
    engine.evaluate("function catchSignal(text) { print ('PROTOTYPE SIGNAL IS CALLED ',text); } p.setText('New String'); " );
    return app.exec();
}
  • 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-11T07:38:53+00:00Added an answer on June 11, 2026 at 7:38 am

    I got rid of the issue, and now I see the signal is being triggered and slot is called properly.

    All I did was moving the code to a separate script file and start using the QScriptDebugger to see its output. Then I figured there was an error and the code is edited to work.

    Anyone who wants an example prototype class, this will hopefully be a good guideline.

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

Sidebar

Related Questions

Tried a bunch of things but I can't get it to work consistently amid
Tried to make a little old school ajax (iframe-javascript) script. A bit of mootools
What is the easiest way to create a global object. I have tried declaring
I'm passing a bool from one form to another, I have tried declaring 'Private
How do you create a global array of strings? I have tried declaring a
tried uncommenting pam_limits.so from the pam.d directory but no luck. Basic PAM seems to
Tried to create a mobile navigation using dataview or outline and the view renders
Tried searching the site, but cannot find an answer to my problem: Lets say
Tried to search for this online and didn't get very far, so I'm asking
Tried following the instructions here: How to use Google app engine with my own

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.