分类: C/C++
2012-04-26 10:45:50
在script脚本中,使用 QObject及其派生类的属性、信号、槽。 练习一界面将Widget的导出到脚本中,名字为widget。 为了将该Widget的3个Children作为属性导出到脚本环境中,需要设置Child的QObject::objectName()
Widget 定义两个参数类型不同的同名信号函数。
设置按钮的Text属性 "widget.btn1.text = 'add number'\n""widget.btn2.text = 'add string'\n" 定义4个函数 "var func1 = function(){widget.textEdit.appendPlainText(Math.random())}\n""var func2 = function(){widget.textEdit.appendPlainText('hello')}\n" "var slot1 = function(text){print(text)}\n" "var slot2 = function(num){print(num)}\n" 连接信号与槽 "widget.btn1.clicked.connect(func1)\n""widget.btn2.clicked.connect(func2)\n" "widget['sig1(QString)'].connect(slot1)\n" "widget['sig1(int)'].connect(slot2)\n" 发射信号 "widget.sig1('from sig1(QString)')\n""widget['sig1(QString)']('from sig1(QString) too')\n" "widget.sig1(798)\n"完整代码 #include #include #include const char script[]="widget.btn1.text = 'add number'\n" "widget.btn2.text = 'add string'\n" "var func1 = function(){widget.textEdit.appendPlainText(Math.random())}\n" "var func2 = function(){widget.textEdit.appendPlainText('hello')}\n" "var slot1 = function(text){print(text)}\n" "var slot2 = function(num){print(num)}\n" "widget.btn1.clicked.connect(func1)\n" "widget.btn2.clicked.connect(func2)\n" "widget['sig1(QString)'].connect(slot1)\n" "widget['sig1(int)'].connect(slot2)\n" "widget.sig1('from sig1(QString)')\n" "widget['sig1(QString)']('from sig1(QString) too')\n" "widget.sig1(798)\n"; class Widget:public QWidget { Q_OBJECT public: Widget(QWidget * parent=NULL):QWidget(parent) { button1 = new QPushButton; button1->setObjectName("btn1"); button2 = new QPushButton; button2->setObjectName("btn2"); QHBoxLayout * hbox = new QHBoxLayout; hbox->addWidget(button1); hbox->addWidget(button2); textEdit = new QPlainTextEdit; textEdit->setObjectName("textEdit"); QVBoxLayout * vbox = new QVBoxLayout(this); vbox->addLayout(hbox); vbox->addWidget(textEdit); } signals: void sig1(const QString& text); void sig1(int number); private: QPushButton * button1; QPushButton * button2; QPlainTextEdit * textEdit; }; #include "main.moc" int main(int argc, char *argv[]) { QApplication app(argc, argv); Widget widget; QScriptEngine engine; QScriptValue w = engine.newQObject(&widget); engine.globalObject().setProperty("widget", w); engine.evaluate(script); widget.show(); return app.exec(); }
#include #include #include const char script[]="print(obj.text)\n" "obj.setText('second')\n" "print(obj.getText())\n" "obj.text = 'third'\n" "print(obj.text)\n"; class HelloObject:public QObject { Q_OBJECT Q_PROPERTY(QString text READ text WRITE setText) public: HelloObject(QObject* parent=NULL) :QObject(parent), m_text("first") {} Q_INVOKABLE QString getText() const { return m_text; } QString text() const { return m_text; } public slots: void setText(const QString& t) { m_text = t; } private: QString m_text; }; #include "main.moc" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); HelloObject obj; QScriptEngine engine; QScriptValue o = engine.newQObject(&obj); engine.globalObject().setProperty("obj", o); engine.evaluate(script); if (engine.hasUncaughtException()){ qDebug()<<engine.uncaughtException().toString(); qDebug()<<engine.uncaughtExceptionBacktrace().join("\n"); } return a.exec(); } 程序输出 firstsecond third |