从C ++更新自定义QML组件

问题描述 投票:0回答:1

我正在使用Opencv Project开发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;

 };

在使用ImageView功能执行ImageService的任何操作后,我需要updateImage()组件进行自我更新。

我尝试过:我想到了:

  1. 将指向ImageService的指针传递给ImageView,但是我不知道如何。
  2. 将ImageService设为Qml Property并将QML图像从Qml传递到ImageView组件,但是我认为这不是一个好主意。
c++ qt opencv qml qqmlcomponent
1个回答
0
投票

如果只有一个ImageView,很容易将其设为模型的Q_PROPERTY

© www.soinside.com 2019 - 2024. All rights reserved.