Qt 4.8.5 QML 与 QT C++ 嵌套

发布于 2024-11-20 08:48:25 字数 2846 浏览 1 评论 0

目前在 Qt4.8.5 中测试成功

Defining New QML Elements

While new QML elements can be defined in QML, they can also be defined by C++ classes; in fact, many of the core QML Elements are implemented through C++ classes. When you create a QML object using one of these elements, you are simply creating an instance of a QObject-based C++ class and setting its properties.

To create a visual item that fits in with the Qt Quick elements, base your class off QDeclarativeItem instead of QObject directly. You can then implement your own painting and functionality like any other QGraphicsObject. Note that QGraphicsItem::ItemHasNoContents is set by default on QDeclarativeItem because it does not paint anything; you will need to clear this if your item is supposed to paint anything (as opposed to being solely for input handling or logical grouping).

For example, here is an ImageViewer class with an image URL property:

 #include <QtCore>
 #include <QtDeclarative>

 class ImageViewer : public QDeclarativeItem
 {
     Q_OBJECT
     Q_PROPERTY(QUrl image READ image WRITE setImage NOTIFY imageChanged)

 public:
     void setImage(const QUrl &url);
     QUrl image() const;

 signals:
     void imageChanged();
 };

Aside from the fact that it inherits QDeclarativeItem, this is an ordinary class that could exist outside of QML. However, once it is registered with the QML engine using qmlRegisterType():

qmlRegisterType<ImageViewer>("MyLibrary", 1, 0, "ImageViewer");

Then, any QML code loaded by your C++ application or plugin can create and manipulate ImageViewer objects:

 import MyLibrary 1.0
 ImageViewer { image: "smile.png" }

It is advised that you avoid using QGraphicsItem functionality beyond the properties documented in QDeclarativeItem. This is because the GraphicsView backend is intended to be an implementation detail for QML, so the QtQuick items can be moved to faster backends as they become available with no change from a QML perspective. To minimize any porting requirements for custom visual items, try to stick to the documented properties in QDeclarativeItem where possible. Properties QDeclarativeItem inherits but doesn't document are classed as implementation details; they are not officially supported and may disappear between releases.

Note that custom C++ types do not have to inherit from QDeclarativeItem; this is only necessary if it is a displayable item. If the item is not displayable, it can simply inherit from QObject.

For more information on defining new QML elements, see the Writing QML extensions with C++ tutorial and the Extending QML Functionalities using C++ reference documentation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

呆°

暂无简介

0 文章
0 评论
23 人气
更多

推荐作者

有深☉意

文章 0 评论 0

硪扪都還晓

文章 0 评论 0

DS

文章 0 评论 0

我也只是我

文章 0 评论 0

TangBin

文章 0 评论 0

橪书

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文