青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

Connecting Signals to QtScript Functions

Posted by Kent on Tuesday, July 17, 2007 @ 19:31

As we all know, connecting to the signals of a QObject from QtScript code is trivial; for example

bossButton.clicked.connect(function() { desktop.showFakeSpreadSheet(); employee.grin(); } );

However, what if you don’t intend to expose your QObject to script code? Let’s say you got this crazy idea, that you only intend for scripters to be able to code some script function (AKA “slot”) that you bring into your application, then you want to handle the actual signal-to-slot connections yourself, in C++ (god forbid!). What do you do then? Scream out in agony?

Well, if you’re using the daily Qt snapshots, there’s actually no need to do so; a couple of days ago we added the necessary functionality. Let’s have a look at the obligatory, completely useless example. First, we have a fair amount of bland code (sorry about that) that sets up a widget with two buttons:

#include <QtGui>
#include <QtScript>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QWidget window;
QVBoxLayout *vbox = new QVBoxLayout(&window);
QPushButton *awakenButton = new QPushButton("Rise From Your Watery Grave");
vbox->addWidget(awakenButton);
QPushButton *exitButton = new QPushButton("Exit");
vbox->addWidget(exitButton);

Then we create our script engine, and a script object, and give the object a suitable name:

    QScriptEngine engine;
QScriptValue object = engine.newObject();
object.setProperty("name", QScriptValue(&engine, "Megatron"));

We read our “slot” script function from a file:

    QFile file(":/slot.qs");
file.open(QIODevice::ReadOnly);
QScriptValue slot = engine.evaluate(file.readAll());
file.close();

Nothing out of the ordinary so far. But now we’re actually ready to do a signal-to-slot connection:

    qScriptConnect(awakenButton, SIGNAL(clicked()), object, slot);

Wow. Just… Wow. How much easier, consistent and intuitive can it get? Both syntactically and semantically, it’s very similar to QObject::connect(). The main difference, of course, is that the last two arguments to qScriptConnect(), the receiver and the slot, are QtScript objects (one can be any type of object, the other must be a function object). When the QtScript “slot” function is executed in response to the signal, the receiver that you specified will act as the this-object. If you specify an invalid QScriptValue as the receiver, the this-object will be the interpreter’s Global Object; this even makes qScriptConnect() consistent with ECMAScript Function.prototype.call() and Function.prototype.apply(), for the purists who care about that stuff (hey, don’t look at me). There is of course also a qScriptDisconnect() function, which destroys a connection.

Finally, demonstrating the principle of “Why do it the easy way when you can do it the hard way”, let’s round off main() by using qScriptConnect() to connect to QApplication::quit() as well:

    QScriptValue scriptApp = engine.newQObject(&app);
qScriptConnect(exitButton, SIGNAL(clicked()), scriptApp, scriptApp.property("quit"));
window.show();
return app.exec();
}

Mmm, beautiful. Last, but not least, the QtScript function loaded from slot.qs, which is executed when the first button is clicked:

return function() {
var sender = __qt_sender__;
sender.text = "YES! " + this.name + " is now alive and kicking!";
sender.styleSheet = "background: lime";
sender["clicked()"].disconnect(this, arguments.callee);
sender.checkable = true;
sender.toggled.connect(sender.hide);
}

First we use a QtScript extension to ECMAScript, __qt_sender__, to grab hold of the button that sent the signal (in general a dubious practice, if you ask me, whether it be script code or C++; but sometimes it’s just too tempting). We change the button’s text, give it a nice background color, then we break the connection that we established in C++ (arguments.callee to the rescue, as usual). Finally we do some more silly stuff, just to show that we can and aren’t afraid to do so.

Oh yeah, in case you were wondering, you can also do a connect from C++ with the QtScript 4.3 API, by doing this:

QScriptValue scriptButton = engine.newQObject(awakenButton);
QScriptValue scriptSignal = scriptButton.property("clicked()");
QScriptValueList args;
args << object << slot;
scriptSignal.property("connect").call(scriptSignal, args);

It’s simple to create a convenience function based on this code, having arguments identical to qScriptConnect(). Way to go 4.3, counterattack!

posted on 2007-10-07 00:20 清源游民 閱讀(2362) 評論(0)  編輯 收藏 引用 所屬分類: Qt
<2007年10月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

留言簿(35)

隨筆分類(78)

隨筆檔案(74)

文章檔案(5)

搜索

  •  

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            99精品欧美一区| 亚洲最新在线| 欧美美女喷水视频| 亚洲精品在线视频| 亚洲国产第一页| 欧美激情一区二区三区在线视频观看| 亚洲国产高清aⅴ视频| 欧美激情一二三区| 欧美日韩国产综合视频在线观看 | 亚洲欧美日韩成人高清在线一区| 亚洲一区二区精品在线| 国产一区二区欧美| 亚洲成色最大综合在线| 国产精品久久久久高潮| 久久亚洲高清| 欧美三级韩国三级日本三斤| 久久国产黑丝| 欧美大胆成人| 欧美一区二区三区四区高清| 欧美aaa级| 亚洲女爱视频在线| 老牛国产精品一区的观看方式| 在线亚洲欧美视频| 久久国产精品一区二区三区四区 | 国产精品欧美精品| 蜜臀av性久久久久蜜臀aⅴ四虎| 欧美精品久久久久久| 久久精品二区亚洲w码| 欧美久久久久免费| 久久久久国产成人精品亚洲午夜| 久久综合色天天久久综合图片| 亚洲特级毛片| 美女精品一区| 久久久久久久成人| 国产精品久久久久三级| 欧美激情精品久久久久久免费印度 | 一区二区三区精品久久久| 一区国产精品| 亚洲欧美视频一区| 一区二区三区毛片| 久久亚洲午夜电影| 久久久久国产成人精品亚洲午夜| 欧美午夜精品| 99re66热这里只有精品4| 亚洲国产欧美一区| 久久久久久9| 久久久久一区二区三区| 国产精品久久久久久久久借妻| 亚洲国产一区二区三区高清 | 国产精品成av人在线视午夜片| 欧美激情va永久在线播放| 国产偷国产偷亚洲高清97cao| 一级成人国产| 99精品国产在热久久下载| 麻豆精品在线视频| 女生裸体视频一区二区三区| 很黄很黄激情成人| 久久riav二区三区| 久久久久久久久久看片| 一区二区黄色| 亚洲主播在线观看| 国产精品美女午夜av| 亚洲专区在线| 久久精品综合| 在线精品一区二区| 蜜桃久久精品一区二区| 亚洲国产成人在线播放| 亚洲毛片在线观看| 欧美日韩国产综合久久| 一本色道久久| 久久久999国产| 在线观看成人av电影| 免费久久精品视频| 亚洲精品少妇30p| 亚洲欧美日韩综合| 国产欧美精品一区aⅴ影院| 久久国产精品久久精品国产| 美女精品一区| 日韩午夜剧场| 国产精品亚洲一区二区三区在线| 亚洲欧美日韩另类| 免费成人av| 亚洲校园激情| 国产亚洲福利一区| 欧美国产91| 亚洲欧美精品一区| 欧美大片网址| 午夜精品福利在线观看| 国产真实乱偷精品视频免| 男女激情视频一区| 亚洲影音先锋| 欧美韩日一区| 欧美一级在线视频| 亚洲日韩中文字幕在线播放| 欧美色欧美亚洲另类七区| 欧美亚洲日本国产| 亚洲欧洲偷拍精品| 久久久久9999亚洲精品| 一区二区久久久久久| 国产在线欧美日韩| 国产精品高潮呻吟视频| 久久久久久9999| 亚洲深夜福利网站| 亚洲国产精品va在线看黑人| 欧美一区二区视频观看视频| 亚洲国产精品成人综合色在线婷婷| 欧美视频手机在线| 欧美成ee人免费视频| 欧美在线一二三四区| 一二三四社区欧美黄| 亚洲高清视频中文字幕| 久久久久久亚洲精品杨幂换脸 | 国产视频在线一区二区| 欧美日韩精品免费观看视频完整| 久久国产视频网| 中国成人在线视频| 亚洲日本中文字幕区| 美女国内精品自产拍在线播放| 亚洲一区免费看| 99re这里只有精品6| 依依成人综合视频| 国产一区二区三区久久精品| 国产精品久久国产三级国电话系列| 美日韩免费视频| 久久在线免费观看| 久久久久久91香蕉国产| 久久国产99| 欧美在线啊v一区| 性欧美精品高清| 亚洲永久字幕| 亚洲欧美成人一区二区三区| 一区二区三区国产| 一区二区日韩精品| 在线中文字幕日韩| 一区二区冒白浆视频| 99在线热播精品免费99热| 亚洲另类在线一区| 日韩视频免费看| 最新日韩欧美| 日韩视频不卡中文| 99re热精品| 亚洲一二三区精品| 先锋亚洲精品| 久久久久久久综合色一本| 久久久久欧美精品| 久久视频在线看| 欧美二区在线播放| 亚洲人体大胆视频| 夜夜嗨一区二区三区| 亚洲免费一在线| 久久国产精品一区二区| 久久综合色88| 欧美日韩国产精品一卡| 欧美日韩另类在线| 国产精品日本一区二区| 国产一区二区看久久| 亚洲电影免费| 一区电影在线观看| 亚洲欧美日韩一区二区| 欧美中文字幕第一页| 牛牛国产精品| 亚洲精品在线二区| 先锋资源久久| 欧美成人精品影院| 国产精品一二三| 在线观看欧美黄色| 亚洲小说区图片区| 久久久www成人免费毛片麻豆| 欧美91大片| 宅男噜噜噜66一区二区66| 欧美一级午夜免费电影| 欧美顶级大胆免费视频| 国产精品视频最多的网站| 亚洲第一精品电影| 亚洲一区www| 欧美肥婆在线| 亚洲欧美久久久| 欧美激情1区2区| 国产日韩精品电影| 99精品热视频| 麻豆精品在线视频| 亚洲一区在线播放| 欧美国产日韩一区二区在线观看 | 亚洲精品亚洲人成人网| 久久xxxx| 一区二区三区成人| 免费欧美在线| 国产在线欧美日韩| 午夜一区不卡| 亚洲精品美女免费| 久久人人爽国产| 国产日韩欧美视频在线| 亚洲无线观看| 亚洲黄色成人久久久| 久久久精品国产一区二区三区 | 在线观看一区| 久久久噜噜噜久久| 午夜欧美精品久久久久久久| 欧美日韩在线播放三区四区| 亚洲国产精品久久久久婷婷884|