Been having some fun this week trying to add support for data bindings to Qt Script, and now I have something that seems to work. This code shows the basic usage:
QScriptValue scriptButton = engine.newQObject(&button);
engine.evaluate("x = 'foo'");
scriptButton.setProperty("text", "x", QScriptValue::DataBinding);
This will cause the button's text property to be updated whenever the script variable
x changes, either from C++ (QScriptValue::setProperty()) or from
script. QObject properties can be part of the binding expression as well, as long as the NOTIFY attribute is specified in the Q_PROPERTY definition. Qt Script will discover that the property has a signal associated with it and use it to track changes. Qt's classes already use NOTIFY for some properties (e.g. QLabel::text).
Below is a small example. The text property of a label is bound to the text property of a lineedit and the script variable
verb; the final value is made by concatenating the lineedit's text, the
verb and a string literal.
int main(int argc, char *argv)
QApplication app(argc, argv);
QVBoxLayout *vbox = new QVBoxLayout(&win);
QLineEdit *lineEdit = new QLineEdit();
QLabel *label = new QLabel();
QScriptValue scriptLineEdit = engine.newQObject(lineEdit);
QScriptValue scriptLabel = engine.newQObject(label);
engine.evaluate("verb = 'smack'");
scriptLabel.setProperty("text", "lineEdit.text + ' ' + verb + 's you!'", QScriptValue::DataBinding);
The result, after typing something into the lineedit and assigning something to
I've also added an extension to QScriptClass that enables you to send property change notifications for custom (non-QObject-based) script objects, i.e. you can use your own notify mechanism.
As I said, this isn't in Qt yet, it's just an experiment, but it's looking pretty interesting.
Download the latest release here: www.qt.io/download.
Qt 5.15 was developed with a strong focus on quality and is a long-term-supported (LTS) release that will be supported for 3 years.
Check out all our open positions here and follow us on Instagram to see what it's like to be #QtPeople.
Näytä tämä julkaisu Instagramissa.
Want to build something for tomorrow, join #QtPeople today! We have loads of cool jobs you don’t want to miss! http://qt.io/careers #builtwithQt #software #developers #coding #framework #tool #tooling #C++ #QML #engineers #sales #tech #technology #UI #UX #CX #Qt #Qtdev #global #openpositions #careers #job
Henkilön Qt (@theqtcompany) jakama julkaisu