Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use QML Technology in C++ Program

2024-05-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to use QML technology in C++ programs, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

Using QML in C++ programs

QML API is divided into three main categories-QDeclarativeEngine QdeclarativeComponent and QDecl arativeContext. QDeclarativeEngine provides an environment in which QML runs QdeclarativeComponent encapsulates QML Documents and QDeclarativeCo

Ntext allows programs to export data to QML component instances. QML also includes a convenience of API through QDeclarativeView applications that simply embed QML components into a new QGraphicsView. There are many details that will be discussed below. QDeclarativeView is mainly used in rapid prototyping applications. If you are re-improving Qt applications that use QML, see integrating QML into existing Qt UI code. Basic usage each application requires at least one QDeclarativeEngine. QDeclarativeEngine allows configuration global settings to be applied to all QML component instances such as QNetworkAccessManager is a path for network communication and permanent storage. If the application requirements differ between QML component instances, only multiple QDeclarati veEngine are required. Use the QDeclarativeComponent class to load QML Documents. Each QDeclarativeComponent instance renders a single QML document. QDeclarativeComponent can pass the address of a document or the original text content of a document. The URL of the document can be the address of the local file system or the network address supported by QNetworkAccessManager. The QML component instance is created by calling the QDeclarativeComponent::create () pattern. Load an example of a Q ML document here and create an object from it. QDeclarativeEngine * engine = new QDeclarativeEngine (parent); QDeclarativeComponent component (engine, QUrl::fromLocalFile ("main.qml")); QObject * myObject = component.create ()

The export data QML component is instantiated in QDeclarativeContext. Context allows applications to export data to the QML component instance. A single QDeclarativeContext can be used for all instance objects of an application or use QDeclarativeContext for each instance to create more precise control over exported data. If you do not pass a context to the QDeclarativeComponent::create () mode, then QDeclarativeEngine's root context will be used. Data export is valid for all object instances through this root context. Simple data sets the Context property in order to export the data to an QML component instance application and is then accessed by the name bound by the QML property and JavaScrip. The following example shows how to export a background color to the

/ / main.cpp # include # include # include in QML file

Int main (int argc, char * argv []) {QApplication app (argc, argv)

QDeclarativeView view; QDeclarativeContext * context = view.rootContext (); context- > setContextProperty ("backgroundColor", QColor (Qt::yellow))

View.setSource (QUrl::fromLocalFile ("main.qml"); view.show ()

Return app.exec ();}

/ / main.qml import Qt 4.7

Rectangle {width: 300 height: 300

Color: backgroundColor

Text {anchors.centerIn: parent text: "Hello Yellow World!"}} or if you need main.cpp without displaying the created components in QDeclarativeView, you need to use Q DeclarativeEngine::rootContext () instead of creating QDeclarativeContext instances. QDeclarativeEngine engine; QDeclarativeContext * windowContext = new QDeclarativeContext (engine.rootContext ()); windowContext- > setContextProperty ("backgroundColor", QColor (Qt::yellow))

QDeclarativeComponent component (& engine, "main.qml"); QObject * window = component.create (windowContext)

The Context property operates like the standard property of the QML binding-in this case the backgroundColor Context property is changed to red and the component object instance is automatically updated. Be careful to delete any QDeclarativeC

The construction of an ontext is a matter for the creator. WindowContext must be destroyed when windowContext is no longer needed when the window component instance is undone. The easiest way is to make sure that it sets window as the parent of windowContext. QDeclarativeContexts is a tree structure-- every QDeclarativeContexts except root context has a parent. The child QDeclarativeContexts effectively inherits the context property of their parent. This gives the application more freedom to separate different data for export to different QML object instances. If QDeclarativeContext sets a context property, its parent is also affected. The new context property is the shadow of the parent. In the following example, the background context attribute is the shadow of Context 1 and the background context attribute in root context.

The structured data context property can also be used to output structured and write data to QML objects. Except that QVariant supports all existing types, QObject derived types can be assigned to the context attribute. The QObject context attribute allows data to be output structurally and allows QML to set values. The following example creates a CustomPalette object and sets it as a palette context property. Class CustomPalette: public QObject {Q_OBJECT Q_PROPERTY (QColor background READ background WRITE setBackground NOTIFY backgroundChanged) Q_PROPERTY (QColor text READ text WRITE setText NOTIFY textChanged)

Public: CustomPalette (): m_background (Qt::white), m_text (Qt::black) {}

QColor background () const {return masked background;} void setBackground (const QColor & c) {if (c! = m_background) {m_background = c; emit backgroundChanged ();}}

QColor text () const {return masked text;} void setText (const QColor & c) {if (c! = m_text) {m_text = c; emit textChanged ();}}

Signals: void textChanged ()

Void backgroundChanged ()

Private: QColor masked background; QColor massively textbook;}

Int main (int argc, char * argv []) {QApplication app (argc, argv)

QDeclarativeView view; view.rootContext ()-> setContextProperty ("palette", new CustomPalette)

View.setSource (QUrl::fromLocalFile ("main.qml"); view.show ()

Return app.exec ();}

QML references the palette object and its properties in order to set the background and text color here the text color of the panel changes to blue when the window is clicked. Import Qt 4.7

Rectangle {width: 240 height: 320 color: palette.background

Text {anchors.centerIn: parent color: palette.text text: "Click me to change color!"}

MouseArea {anchors.fill: parent onClicked: {palette.text = "blue";}

You can detect the value of a C++ property-in this case, the text property of the CustomPalette must have the corresponding NOTIFY information to change the property. A NOTIFY signal is specified to be emitted when the attribute value is changed. The implementer should pay attention to the fact that the signal is transmitted only when the value is changed to prevent an endless loop. Access a bound property

The absence of a NOTIFY signal will cause QML to issue a warning message at run time. Dynamic structured data if the application compiles QObject types too dynamically for structuralization, then dynamic structured data can be constructed with QDeclarativePropertyMap classes at run time.

Calling C++ from QML enables it to call QObject-derived class types through public slots output mode or Q_INVOKABLE tag mode. C++ mode can also have parameters and return values. QML supports the following types? bool? unsigned int, int? float, double, qreal? QString? QUrl? QColor? QDate,QTime,QDateTime? QPoint,QPointF? QSize,QSizeF? QRect,QRectF? QVariant the following example demonstrates the switch that controls the "Stopwatch" object when MouseArea clicks. / / main.cpp class Stopwatch: public QObject {Q_OBJECT public: Stopwatch ()

Q_INVOKABLE bool isRunning () const

Public slots: void start (); void stop ()

Private: bool masking running;}

Int main (int argc, char * argv []) {QApplication app (argc, argv)

QDeclarativeView view; view.rootContext ()-> setContextProperty ("stopwatch", new Stopwatch)

View.setSource (QUrl::fromLocalFile ("main.qml"); view.show ()

Return app.exec ();}

/ / main.qml

Import Qt 4.7

Rectangle {width: 300 height: 300

MouseArea {anchors.fill: parent onClicked: {if (stopwatch.isRunning ()) stopwatch.stop () else stopwatch.start ();

It's worth noting that there are better ways to achieve the same effect in this particular case where main.qml has a "run ning" attribute, which would be a very good QML code / / main.qml import Qt 4.7.

Rectangle {

MouseArea {anchors.fill: parent onClicked: stopwatch.running =! stopwatch.running}}

Of course, it can also call functions declared in QML from C++.

Network component if the URL passed to QDeclarativeComponent is a network resource or the QML document refers to a network resource QDeclarativeComponent must obtain the network data before creating an object. In this case, QDecl arativeComponent will have Loading status. The application waits until the component calls QDeclarativeComponent::create (). The following example shows how to load a QML file from a network resource. After creating the QDeclarativeComponent, it tests whether the component is loaded. If it connects to the QDeclarativeComponent::statusChanged () signal number, then call continueLoading () directly. This test is necessary and even the URL can be remote just in case the component is cached. MyApplication::MyApplication () {/ / … Component = new QDeclarativeComponent (engine, QUrl ("http://www.example.com/mai n.qml")); if (component- > isLoading ()) QObject::connect (component, SIGNAL (statusChanged (QDeclarativeComponent::Statu s)), this, SLOT (continueLoading ()); else

ContinueLoading ();}

Void MyApplication::continueLoading () {if (component- > isError ()) {qWarning () errors ();} else {QObject * myObject = component- > create ();}}

The contents of the Qt resource QML can be loaded from the Qt resource system using the qrcURL scheme. For example, [project/example.qrc]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report