QML image not refreshing when evoked in separate method
Im tring to update a qml Image URL via signal:
This is working perfectly fine when executed in this method:
Code:
void Sentinel::startQmlEngine()
{
QQmlApplicationEngine qmlEngine;
qmlRegisterType<ImageItem>("ImageItem",1,0,"ImageItem");
qmlEngine.rootContext()->setContextProperty("imageUrlClass", &image);
QQmlComponent component(&qmlEngine, "qrc:/main.qml");
object = component.create();
assert(object);
image.setUrl(getPathToImage());
}
As soon as I outsource setting the URL the signal is not emitted
Code:
void Sentinel::updateGui()
{
image.setUrl(getPathToImage());
}
How can this be?
Here my Qml
Code:
import QtQuick 2.12
import QtQuick.Window 2.12
import ImageItem 1.0
Rectangle {
width: 768
height: 576
Connections {
target: imageUrlClass
onUrlChanged: {
image.update();
console.log("image UURL-" + imageUrlClass.imageUrl);
}
}
Image {
id: image
objectName: "image"
x: 0
y: 0
width: 768
height: 576
source: imageUrlClass.imageUrl
opacity: 1
clip: true
fillMode: Image.PreserveAspectFit
cache: false
}
}
ImageItem class header:
Code:
#pragma once
#include <QQuickPaintedItem>
//---------------------------------------------------------------------------
class ImageItem: public QQuickPaintedItem
{
Q_OBJECT
Q_PROPERTY(QVariant imageUrl READ getUrl
() WRITE setUrl NOTIFY urlChanged
)
public:
explicit ImageItem(QQuickItem *parent = nullptr);
signals:
void urlChanged();
private:
};
And Cpp:
Code:
#include "ImageItem.h"
//---------------------------------------------------------------------------
ImageItem::ImageItem(QQuickItem *parent) :
QQuickPaintedItem(parent)
{
}
{
return url;
}
void ImageItem
::setUrl(const QVariant &value
) {
url = value;
emit urlChanged();
}
void ImageItem
::paint(QPainter* painter
) {
paint(painter);
}
Thanks!
Re: QML image not refreshing when evoked in separate method
In startQmlEngine(), you declare an instance of QQmlApplicationEngine as a local variable inside the method. As soon as the method ends, this local variable goes out of scope and is destroyed. Any components created using that engine instance are also destroyed, and any signal / slot connections made are automatically disconnected.
Re: QML image not refreshing when evoked in separate method
Quote:
Originally Posted by
d_stranz
In startQmlEngine(), you declare an instance of QQmlApplicationEngine as a local variable inside the method. As soon as the method ends, this local variable goes out of scope and is destroyed. Any components created using that engine instance are also destroyed, and any signal / slot connections made are automatically disconnected.
Thanks you are genius.
Re: QML image not refreshing when evoked in separate method
Just some additional observation which might not be applicable depending on how your final code will look like:
* you don't need to derive from QQuickItem if you are only using the object for transporting data via properties
* if the property is a URL then the C++ type for this would be QUrl
* you only need qmlRegisterType() and the import if you are instantiating the type from within QML or if the type provides enums that you want to access by name
Cheers,
_