QT qScriptConnect 不工作

QT qScriptConnect is not working

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);

mainwindow.h

#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

mainwindow.cpp

#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 是在构造函数中创建的,原因是它被删除了,所有脚本取决于它。解决方法很简单,让class的QScriptEngine成员:

mainwindow.h

#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

mainwindow.cpp

#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;
}