Download RV Manual - Tweak Software
Transcript
CHAPTER 3. USING QT IN MU
3.1
Signals and Slots
Possibly the biggest difference between the Mu and C++ Qt API is how signals and slots are handled. This
discussion will assume knowledge of the C++ mechanism. See the Qt documentation if you don’t know what
signals and slots are.
Jumping right in, here is an example hello world MuQt program. This can be run from the mu-interp
binary:
use qt;
\: clicked (void; bool checked)
{
print("OK BYE\n");
QCoreApplication.exit(0);
}
\: main ()
{
let app
= QApplication(string[] {"hello.mu"}),
window = QWidget(nil, Qt.Window),
button = QPushButton("MuQt: HELLO WORLD!", window);
connect(button, QPushButton.clicked, clicked);
window.setSize(QSize(200, 50));
window.show();
window.raise();
QApplication.exec();
}
main();
The main thing to notice in this example is the connect() function. A similar C++ version of this
would look like this:
connect(button, SIGNAL(clicked(bool)), SLOT(myclickslot(bool)));
where myclickslot would be a slot function declared in a class. In Mu its not necessary to create
a class to recieve a signal. In addition the SIGNAL and SLOT syntax is also unnecessary. However, it is
necessary to exactly specify which signal is being referred to by passing its Mu function object directly. In
this case QPushButton.clicked. The signal must be a function on the class of the first argument of
connect().
In Mu, any function which matches the signal’s signature can be used to receive the signal. The downside
of this is that some functions like sender() are not available in Mu. However this is easily overcome with
partial application. In the above case, if we need to know who sent the signal in our clicked function, we can
change its signature to accept the sender and partially apply it in the connect call like so:
\: clicked (void; bool checked, QPushButton sender)
{
// do something with sender
}
\: main ()
{
...
connect(button, QPushButton.clicked, clicked(,button));
20