QScriptEngine eng;
QScriptEngineDebugger debugger;
debugger.attachTo(&eng);
QScriptValue consoleObj =  eng.newQObject(this);
eng.globalObject().setProperty("asd", consoleObj);
QScriptValue handler= eng.evaluate("(function(text) { asd.log('text was changed to '+text); })");
QLineEdit *edit = new QLineEdit(this);
qScriptConnect(edit, SIGNAL(textChanged(QString)), QScriptValue(), handler);
edit->show();


qScriptConnect(edit, SIGNAL(textChanged(QString)), QScriptValue(), handler)不起作用,但是当我使用handler.call(QScriptValue(),args)时,它运行良好;

QScriptValueList args;
args << 3;
handler.call(QScriptValue(),args);


主窗口

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
      Q_INVOKABLE void log(QString msg);
    ~MainWindow();

private slots:

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H


主窗口

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<qscriptengine.h>
#include<qscriptenginedebugger.h>
#include<qlineedit.h>
#include<qdebug.h>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QScriptEngine eng;
    QScriptEngineDebugger debugger;
    debugger.attachTo(&eng);
    QScriptValue consoleObj =  eng.newQObject(this);
    eng.globalObject().setProperty("asd", consoleObj);
    QScriptValue handler= eng.evaluate("(function(text) { asd.log('text was changed to '+text); })");
    QLineEdit *edit = new QLineEdit(this);
    qDebug()<<qScriptConnect(edit, SIGNAL(textChanged(QString)), QScriptValue(), handler);
    edit->show();
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::log(QString msg)
{
    qDebug() << "jsConsole: "<< msg;
}

最佳答案

当在方法中创建的未在堆中创建的变量执行完该方法后,该变量将被删除,在您的情况下,QScriptEngine是在构造函数中创建的,因此它被删除了,所有脚本都依赖于它。解决方案很简单,使该类的QScriptEngine成员:

主窗口

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QScriptEngine>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    Q_INVOKABLE void log(const QString & text);

private:
    Ui::MainWindow *ui;
    QScriptEngine eng; // member
};

#endif // MAINWINDOW_H


主窗口

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QLineEdit>
#include <QScriptEngine>
#include <QScriptEngineDebugger>

#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QScriptEngineDebugger debugger;
    debugger.attachTo(&eng);
    QScriptValue consoleObj =  eng.newQObject(this);
    eng.globalObject().setProperty("asd", consoleObj);
    QScriptValue handler= eng.evaluate("(function(text) { asd.log('text was changed to '+text); })");
    QLineEdit *edit = new QLineEdit(this);
    qScriptConnect(edit, SIGNAL(textChanged(QString)), QScriptValue(), handler);
    edit->show();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::log(const QString &text)
{
    qDebug()<< text;
}

10-02 07:30