QT QDeclarativeImageProvider child class single instance?
Hi,
has someone else seen this?
I have a
class QMyImgProvClass : public QDeclarativeImageProvider{
..
virtual QImage requestImage(const QString &id, QSize *size, const QSize& requestedSize){
...
}
QMyImgProvClass * instance(){
static QMyImgProvClass s_myClass;
return &s_myClass;
}
public:
QList<int> m_list;
};
Since I need to use this in a plugin, I register it in the
void BookmarkModelPluginPlugin::initializeEngine(QDeclarativeEngine *engine, const char *uri){
engine->addImageProvider(QLatin1String(MY_IMGPROV_NAME), QMyImgProvClass::Instance());
}
through a simple Singleton.
In the plugin then, I populate the m_list;
.
Now, the requestImage is called correctly from QML. BUT, the effect is that the m_list list is EMPTY for each call. It seems that for each call to the requestImage, the "this" pointer is different.
I thought the addImageProvider registers a single object as an image provider?
Re: QT QDeclarativeImageProvider child class single instance?
Are you populating m_list before opening any QML file?
Br,
Villep
Re: QT QDeclarativeImageProvider child class single instance?
From inside ::componentComplete of the plugin.
Re: QT QDeclarativeImageProvider child class single instance?
What ::componentComplete are you referring to? Component.onCompleted inside QML? How are you trying to populate m_list, C++ side or inside some QML file?
Br,
Villep
Re: QT QDeclarativeImageProvider child class single instance?
[QUOTE=tkastlunger;902222]From inside ::componentComplete [B]of the plugin[/B].[/QUOTE]
How would I be able to populate the plugin from QML with C++? Would be intresting to know :).
Re: QT QDeclarativeImageProvider child class single instance?
How are you calling ::componentComplete? You can also try adding some variable in your that you increment each time requestImage is called to make sure it gets incremented with a single instance.
Something like
[CODE]
#include <QDebug>
class QMyImgProvClass : public QDeclarativeImageProvider {
public:
QMyImgProvClass() : mNumberOfCalls(0)
{
}
...
virtual QImage requestImage(const QString &id, QSize *size, const QSize& requestedSize){
...
++mNumberOfCalls;
qDebug() << "Calls:" << mNumberOfCalls;
}
public:
QList<int> m_list;
private:
int mNumberOfCalls;
};
[/CODE]
Br,
Villep
Re: QT QDeclarativeImageProvider child class single instance?
::componentComplete is called by the QML backend, I personally do not call it directly.
The strange thing is that the list _is_ populated, if I access it through the singleton. If I access it from within the requestImage, as in:
virtual QImage requestImage(const QString &id, QSize *size, const QSize& requestedSize){
...
if (m_list.size() > 0)
// this is never reached because m_list is empty
}
whilst this in turn:
virtual QImage requestImage(const QString &id, QSize *size, const QSize& requestedSize){
...
if (QMyImgProvClass::Instance().m_list.size() > 0)
// this is reached just fine.
}