• <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>

            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 清源游民 閱讀(2313) 評論(0)  編輯 收藏 引用 所屬分類: Qt
            <2009年5月>
            262728293012
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            留言簿(35)

            隨筆分類(78)

            隨筆檔案(74)

            文章檔案(5)

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            久久久久精品国产亚洲AV无码| 人妻无码久久精品| 国产情侣久久久久aⅴ免费| 人妻精品久久无码专区精东影业 | 国产高潮国产高潮久久久| 久久国产精品成人影院| 国产精品激情综合久久| 色8激情欧美成人久久综合电| 久久精品国产乱子伦| 中文字幕久久欲求不满| 亚洲国产精品一区二区三区久久 | 无码人妻少妇久久中文字幕蜜桃| 精品久久久久香蕉网| 青青草国产97免久久费观看| 久久午夜福利无码1000合集| 人人狠狠综合久久亚洲婷婷| 国产精品一区二区久久精品无码 | 亚洲综合精品香蕉久久网| 久久被窝电影亚洲爽爽爽| 久久99国产精品久久99小说 | 久久久青草青青国产亚洲免观| 亚洲色婷婷综合久久| 日韩中文久久| 久久久久女教师免费一区| 精品久久久久久国产91| 日本久久久久亚洲中字幕| 综合久久一区二区三区| 久久免费观看视频| 久久国产精品二国产精品| 欧美综合天天夜夜久久| 精品999久久久久久中文字幕| 亚洲国产精品久久久天堂| 久久精品国产99久久久古代 | 久久精品成人国产午夜| 国产综合久久久久| 久久久青草青青亚洲国产免观| 日韩av无码久久精品免费| 伊人久久精品无码二区麻豆| 久久久亚洲欧洲日产国码是AV| 久久久久久精品成人免费图片| 免费无码国产欧美久久18|