我正在使用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()
组件进行自我更新。
我尝试过:我想到了:
ImageService
的指针传递给ImageView,但是我不知道如何。Qml Property
并将QML图像从Qml传递到ImageView
组件,但是我认为这不是一个好主意。 如果只有一个ImageView
,很容易将其设为模型的Q_PROPERTY
。