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

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>
            亚洲欧洲午夜| 曰本成人黄色| 亚洲午夜一级| 9i看片成人免费高清| 欧美亚州在线观看| 欧美一区亚洲一区| 欧美在线不卡| 亚洲精品国产欧美| 一本色道久久综合亚洲精品按摩 | 美女图片一区二区| 久久综合久久综合久久| 亚洲美女精品一区| 亚洲欧美国产精品专区久久| 国产有码一区二区| 亚洲国内在线| 国产精品一区视频网站| 久久一区二区三区四区五区| 欧美黄色aa电影| 亚洲午夜免费视频| 久久精品一二三| 一区二区三区久久网| 亚洲欧美日韩一区二区在线 | 欧美日韩美女| 久久久青草婷婷精品综合日韩| 美女性感视频久久久| 亚洲综合国产激情另类一区| 日韩午夜激情| 欧美一区二区三区在线观看视频 | 一区二区三区 在线观看视频 | 亚洲精品国产拍免费91在线| 欧美日韩中文字幕精品| 久久久水蜜桃| 国产精品成人一区二区| 欧美大尺度在线| 国产精品无人区| 亚洲欧洲一区二区在线播放| 国产伦精品一区二区三区在线观看 | 国产精品一区二区a| 亚洲国产精品久久精品怡红院| 国产精品一区久久久| 亚洲精品九九| 亚洲电影在线看| 亚洲欧美日韩一区二区三区在线观看 | 欧美一区二区三区啪啪| 中文日韩欧美| 欧美1区视频| 玖玖玖免费嫩草在线影院一区| 国产精品每日更新| 亚洲伦理在线免费看| 91久久国产自产拍夜夜嗨| 久久成人免费日本黄色| 欧美在线一级va免费观看| 欧美视频1区| 亚洲日韩视频| 亚洲美女少妇无套啪啪呻吟| 久久亚洲美女| 欧美成人黄色小视频| 激情久久综合| 久久久久国产一区二区三区| 久久成人精品无人区| 国产精品青草综合久久久久99| 99re6这里只有精品视频在线观看| 亚洲国产一区二区精品专区| 久久久综合网| 欧美激情1区2区3区| 在线播放日韩欧美| 老司机久久99久久精品播放免费 | 一区二区三区四区五区精品视频| 一本色道久久综合亚洲精品高清| 欧美成年人网| 91久久精品国产91性色tv| 夜夜爽99久久国产综合精品女不卡 | 亚洲欧美区自拍先锋| 午夜精品久久久久久久男人的天堂| 欧美视频日韩视频| 亚洲欧美国产另类| 久久夜色精品国产欧美乱| 好吊色欧美一区二区三区四区| 欧美在线一二三区| 农夫在线精品视频免费观看| 91久久精品国产91久久性色| 欧美国产日产韩国视频| 一本色道久久综合亚洲91| 欧美主播一区二区三区| 黄色成人av| 久久精品国产亚洲aⅴ| 国产一区再线| 久久亚洲风情| 亚洲美女黄色片| 欧美亚洲一区二区在线| 一区二区三区中文在线观看| 欧美国产第二页| 亚洲一区二区成人在线观看| 久久久久九九九九| 日韩视频在线一区| 国产精品有限公司| 免费欧美日韩| 亚洲一区二区三区成人在线视频精品| 久久精品最新地址| 一本色道久久88精品综合| 国产日本欧美一区二区| 免费亚洲电影| 欧美一区二区三区精品| 亚洲国产你懂的| 欧美中文在线视频| 亚洲久久一区二区| 国产欧美在线播放| 欧美精品激情| 久久成人国产精品| 夜夜夜精品看看| 欧美成人xxx| 欧美在线视频不卡| 一本大道久久a久久精品综合| 国产日韩欧美综合在线| 欧美日韩免费| 蜜桃伊人久久| 久久久久久久波多野高潮日日 | 久久精品毛片| 亚洲影院在线观看| 亚洲精品国久久99热| 国内精品国语自产拍在线观看| 欧美激情乱人伦| 久久综合九色欧美综合狠狠| 亚洲欧美在线免费| 一本久道久久综合中文字幕 | 午夜精品国产| 夜夜夜久久久| 亚洲久色影视| 亚洲欧洲一区二区三区| 一区二区亚洲精品国产| 国产欧美日韩高清| 国产精品手机视频| 欧美日韩午夜剧场| 欧美日韩国产va另类| 欧美大片网址| 欧美成人午夜激情在线| 久久综合狠狠| 久久在线精品| 噜噜噜在线观看免费视频日韩| 久久久久欧美| 久久在线免费视频| 久久综合久色欧美综合狠狠 | 亚洲七七久久综合桃花剧情介绍| 老司机亚洲精品| 麻豆乱码国产一区二区三区| 狂野欧美激情性xxxx欧美| 久久久综合香蕉尹人综合网| 久久影音先锋| 欧美成人精品在线观看| 欧美福利影院| 亚洲人成网站在线播| 亚洲精品一区在线| 亚洲天堂av在线免费| 亚洲婷婷在线| 欧美在线首页| 久久视频这里只有精品| 蜜桃久久av一区| 欧美日韩福利视频| 国产精品入口| 狠狠色综合网| 99av国产精品欲麻豆| 亚洲尤物在线| 欧美另类极品videosbest最新版本| 欧美二区乱c少妇| 欧美色图天堂网| 国产视频观看一区| 亚洲国产精品久久91精品| 亚洲美女中出| 性欧美暴力猛交69hd| 久久综合五月天婷婷伊人| 亚洲二区视频| 亚洲一区二区三区中文字幕在线| 欧美综合77777色婷婷| 免费亚洲一区| 国产精品久久久久久久浪潮网站 | 亚洲精品一区中文| 亚洲欧美电影院| 欧美1区3d| 亚洲天堂av在线免费| 鲁大师影院一区二区三区| 欧美日韩亚洲综合| 极品中文字幕一区| 亚洲视频在线观看一区| 久久影院亚洲| 亚洲午夜精品久久久久久浪潮| 久久青草欧美一区二区三区| 国产精品都在这里| 在线观看欧美视频| 欧美影院视频| 99国产一区| 老司机午夜免费精品视频| 国产精品一区二区三区观看| 亚洲每日更新| 媚黑女一区二区| 亚洲综合99| 欧美日韩在线不卡一区| 亚洲国产精品黑人久久久| 久久高清免费观看| 夜夜狂射影院欧美极品| 欧美丰满高潮xxxx喷水动漫|