简体   繁体   English

Qt Qml Javascript-在哪里使用?

[英]Qt Qml Javascript - Where to use which?

I'm new to Qt and Qml and I'm trying to figure out how they work together. 我是Qt和Qml的新手,我想弄清楚它们如何协同工作。
I'm confused about where to use C++ and where to use JavaScript. 我对在哪里使用C ++和在哪里使用JavaScript感到困惑。 Let's say I have a couple of QML objects (like forms, inputs, dropdowns, etc.). 假设我有几个QML对象(例如表单,输入,下拉列表等)。 Now obviously these components have some logic code. 现在显然这些组件具有一些逻辑代码。 Should I write this logic code in JavaScript or in C++? 我应该用JavaScript还是C ++编写此逻辑代码? Let's say my input has a group of properties and signals. 假设我的输入具有一组属性和信号。 Where and how should these be coded? 这些应该在哪里以及如何编码? If I should write it in JavaScript, then how C++ is used. 如果我应该用JavaScript编写它,那么C ++的用法。 How C++ and JavaScript are connected? C ++和JavaScript如何连接? I'm pretty confused! 我很困惑! and the docs doesn't help me either. 而文档也没有帮助我。 Thanks 谢谢

The QML language was invented to be used to describe interfaces. 发明了QML语言来描述接口。 It is meant to be simple to understand for designers. 对于设计师来说,这意味着易于理解。

This mean that in a program you will generally have all the logic implemented in C++ and the UI implemented in QML. 这意味着在程序中,通常将具有用C ++实现的所有逻辑,以及具有QML实现的UI。 To make the link between C++ and QML it is necessary to have some C++ code exposed to QML. 为了在C ++和QML之间建立链接,必须将一些C ++代码公开给QML。 There a many way to do that. 有很多方法可以做到这一点。 For instance you could make a C++ class available in QML (see http://doc.qt.io/qt-5/qtqml-cppintegration-topic.html ), just make the instance of a singleton available in QML or inject a QObject pointer into the QML environment. 例如,您可以使C ++类在QML中可用(请参阅http://doc.qt.io/qt-5/qtqml-cppintegration-topic.html ),只需使单例实例在QML中可用或注入QObject指向QML环境的指针。 All of this heavily use the Qt meta object system ( http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html ). 所有这些都大量使用Qt元对象系统( http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html )。

// in C++
class MyClass
{
    Q_OBJECT
public slots:
    int doSomething();
   ...
};

int main()
{
...
   engine->rootContext()->setContextProperty("foo", new MyClass());
...
}

// in QML

function foobar() {
    var anInt = foo.doSomething();
}

QML allowing you to write javascript you can also write a complete program without ever using C++, implementing everything in javascript. QML允许您编写javascript,您也可以编写完整的程序而无需使用C ++,从而可以用javascript实现所有功能。 But this is generally a bad idea, especially if you need performances. 但这通常是一个坏主意,尤其是在您需要表演时。

Unlike some say, C++ is not here for extending QML. 与某些人不同,C ++并不是在这里扩展QML。 But QML is here to offer a simple interface to C++ objects, allowing developers and designers to create fancy UI without typing/learning C++. 但是QML在这里为C ++对象提供了一个简单的界面,使开发人员和设计人员无需键入/学习C ++即可创建精美的UI。

My personal rule when writing QML is to go to C++ as soon as possible. 在编写QML时,我个人的规则是尽快使用C ++。 Sure you can write simple functions in QML, but you have to keep them short and simple to leverage the full power of the optimizations of the underlying QML and JS engines. 当然,您可以在QML中编写简单的函数,但是必须使它们简短而简单,以充分利用基础QML和JS引擎优化的全部功能。 Keeping QML fast is so hard there is a full page in Qt documentation of what to think about when using QML ( http://doc.qt.io/qt-5/qtquick-performance.html ). 保持QML快速非常困难,Qt文档中有一整页介绍了使用QML时的注意事项( http://doc.qt.io/qt-5/qtquick-performance.html )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM