从C++更新自定义QML组件
Update Custom QML component from from C++
我正在使用Opencv项目开发一个c++/QML。。我发现在QMl中查看处理后的图像的最佳方式是编写一个自定义QMl组件,该组件在c++中扩展了QQuickPaintedItem
,并且运行良好:
class ImageView : public QQuickPaintedItem{
Q_OBJECT
Q_PROPERTY(QImage image READ image WRITE setImage NOTIFY imageChanged)
public:
// I need to pass a pointer of ImageService
ImageView(QQuickItem *parent = nullptr, ImageService *imageService = nullptr);
Q_INVOKABLE void updateImage();
void paint(QPainter *painter);
QImage image() const;
signals:
void imageChanged();
private:
QImage m_image;
ImageService *m_imageService;
};
我在C++中注册了这种类型,如下所示:
qmlRegisterType<ImageView>("opencv.plugin", 1, 0, "ImageView");
我的问题是:我有一个类ImageService
,它完成所有的工作,并保存处理后的图像的最后版本:
class ImageService
{
public:
ImageService();
bool openImage(const std::string &);
QImage toQImage();
bool isValid();
private:
std::string m_imagePath;
cv::Mat m_image;
};
在ImageService
使用updateImage()
函数执行任何操作之后,我需要ImageView
组件更新自己。
我试过:我想过:
- 将指向
ImageService
的指针传递给ImageView,但我不知道该怎么做 - 将ImageService作为
Qml Property
,并将QML图像从QML传递到ImageView
组件,但我认为这不是一个好主意
如果只有一个ImageView
,那么很容易将其作为模型的Q_PROPERTY
。
class ImageModel : public QObject
{
Q_OBJECT
Q_PROPERTY(ImageView *image1 READ image1 CONSTANT)
public:
ImageModel(ImageService *service)
: service_(service)
, image1_(new ImageView(/*parent=*/ this, /*service=*/service_)
{ }
ImageView* image1() const { return image1_; }
private:
ImageService *service_;
ImageView *image1_;
}
更改主菜单:
...
qmlRegisterUncreatableType<ImageService>(...);
qmlRegisterUncreatableType<ImageView>(...);
qmlRegisterUncreatableType<ImageModel>(...);
QQmlEngine engine;
ImageService theService;
ImageModel coreModel(&theService);
QQmlContext *context = new QQmlContext(engine.rootContext());
context->setContextProperty("images", &coreModel);
您可以使用的qml中:
<imageproperty>: images.image1
相关文章:
- 基于 QML 模型的组件在 android 上具有未定义的 modelData
- 是否可以从C++创建 Qml 组件?
- 如何在 QML 组件中使用 C++ 类
- QML 如何捕获组件转换更改
- Qml:从文件加载组件时将参数作为 URL 的一部分传递
- QML组件与信号连接
- 从C++访问现有的 QML 组件
- 如何将本机窗口嵌入到 QML 组件中
- QML:如何将动态创建的组件与自定义的内部对象一起使用
- 如何在 QML 中实例化C++组件 - WITH 参数化构造函数
- QtQuick:QQmlApplicationEngine无法加载组件qrc:/main.qml:23附加对象分配无效
- qml桌面组件扩展
- QML 从C++在加载器中动态插入组件
- 如何使用 qml 组件(不带 C++)创建插件的 deb 包
- QML:如何在不实际移动任何可见组件的情况下,在鼠标Aeria顶部获取鼠标移动的方向
- 无法更改从C++动态创建的 QML 组件的属性
- 设计Qml桌面组件的样式
- 从c++中与Repeater中委托的QML组件进行交互
- QML组件'Video'无法播放Qt资源文件中的视频文件
- 如何模拟QML组件