[英]Update Custom QML component from from C++
I am working on a c++/QML with Opencv Project.. I have found that the best way to view the processed image in QMl is to write a custom QML component that extends QQuickPaintedItem
in c++ and it is working well: I am working on a c++/QML with Opencv Project.. I have found that the best way to view the processed image in QMl is to write a custom QML component that extends QQuickPaintedItem
in c++ and it is working well:
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;
};
and I registered this type in C++ like the following:我在 C++ 中注册了这种类型,如下所示:
qmlRegisterType<ImageView>("opencv.plugin", 1, 0, "ImageView");
My Problem is: I have this class ImageService
that does all the work and holds the last version of the image after processing:我的问题是:我有这个 class ImageService
完成所有工作并在处理后保存图像的最新版本:
class ImageService
{
public:
ImageService();
bool openImage(const std::string &);
QImage toQImage();
bool isValid();
private:
std::string m_imagePath;
cv::Mat m_image;
};
I need ImageView
component to update itself after any operation that ImageService
perform using updateImage()
function.在ImageService
使用updateImage()
function 执行任何操作后,我需要ImageView
组件来更新自身。
I tried: I thought about:我试过:我想过:
ImageService
to ImageView, But I do not see how.将指向ImageService
的指针传递给 ImageView,但我不知道如何。Qml Property
and pass a QML Image from Qml to the ImageView
component,But I do not think that it is a good idea. Making ImageService as Qml Property
and pass a QML Image from Qml to the ImageView
component,But I do not think that it is a good idea.In case you only have one ImageView
it is easy to make it a Q_PROPERTY
of a model.如果您只有一个ImageView
,很容易将其设置为 model 的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_;
}
Change your main:改变你的主要:
...
qmlRegisterUncreatableType<ImageService>(...);
qmlRegisterUncreatableType<ImageView>(...);
qmlRegisterUncreatableType<ImageModel>(...);
QQmlEngine engine;
ImageService theService;
ImageModel coreModel(&theService);
QQmlContext *context = new QQmlContext(engine.rootContext());
context->setContextProperty("images", &coreModel);
The in qml you can use:在 qml 你可以使用:
<imageproperty>: images.image1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.