Solved requestPixmap causes moc errors
-
Hi,
I have the following code in my makepix.h:/#ifndef MAKEPIX_H #define MAKEPIX_H #include <QObject> #include <QQuickImageProvider> class MakePix : public QObject, public QQuickImageProvider { Q_OBJECT public: MakePix() : QQuickImageProvider(QQuickImageProvider::Pixmap) {} public slots: void getImgPath(const QString& path); QPixmap requestPixmap(const QString& id, QSize* size, const QSize& requestedSize) override; }; #endif // MAKEPIX_H
It generates the following error message:
moc_makepix.cpp:-1: error: undefined reference toMakePix::requestPixmap(QString const&, QSize*, QSize const&)' moc_makepix.cpp:-1: error: undefined reference to
non-virtual thunk to MakePix::requestPixmap(QString const&, QSize*, QSize const&)'collect2.exe:-1: error: error: ld returned 1 exit status.
If I comment outQPixmap requestPixmap(const QString& id, QSize* size, const QSize& requestedSize) override;
There is no error message and it builds and runs OK. How can I fix this error?
Thank you for your help. -
Hi,
You did not provide the implementation of that method hence the error. If you declare slots like that, you have to have at least an empty implementation of it.
By the way, it's not a slot in the base case, there's not reason to change that in a subclass.
-
Hi @SGaist,
I implemented requestPixmap and it is working now.Would you please explain what you meant by "it's not a slot in the base case, there's not reason to change that in a subclass."
Thank you for your help.
-
@gabor53
Hi
You put requestPixmap function under slots:
public slots:
QPixmap requestPixmap(const QString& id, QSize* size, const QSize& requestedSize) override;but its not a slot in the base class ( base class is QQuickImageProvider in your case )
its a virtual function one can override. ( as you did)
and i assume its public.
https://doc.qt.io/qt-5/qquickimageprovider.html#requestPixmap
so its not a slot, so no reason to make it a slot in your class. -
Hi ,
I removed requestPixmap from public slots. Thank you.
How can I callQPixmap requestPixmap(const QString& id, QSize* size, const QSize& requestedSize) override;
Thank you.
-
@gabor53 said in requestPixmap causes moc errors:
QQuickImageProvider
Its explained the docs for the class
https://doc.qt.io/qt-5/qquickimageprovider.html#detailsYou will use the name in the source statement
Image { source: "image://MyImageProvider/Image.png" }note:
- I removed requestPixmap from public slots
Meaning you have moved it to public (non slot) section, i assume.
- I removed requestPixmap from public slots
-
@mrjj ,
I understand now how QML will call QQuickimageProvider. But, how can I pass the image id(path) from QML?
Thank you for your help. -
@gabor53
Hi
in what way pass ?- But, how can I pass the image id(path) from QML?
When you do
Image { source: "image://MyImageProvider/Image.png" }
i assume you will see Image.png as id. -
@mrjj,
Yes, but should I use a signal/slot to send the path from QML to C++ and call QQuickImageProvider from that slot?
Thank you. -
Hi
Nope. you register it.
"The QML engine invokes the appropriate image provider according to the providers that have been registered through QQmlEngine::addImageProvider()."
https://doc.qt.io/qt-5/qqmlengine.html#addImageProviderThen when you do
Image { source: "image://MyImageProvider/Image.png" }
if you have registered as MyImageProvider , it will automatically be called. -
Hi,
No, take a look again at the QQuickimageProvider documentation.
It shows how to use it from QML:
Image { source: "image://myimageprovider/image.png" }
-
@SGaist
Thank you. It works now.