版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/keneyr/article/details/89153585
就是在运行QT给出的官方示例的时候,告诉我这个错误。。尼玛把我给急死了。。。死活找不到错误。这怎么还无效呢?当时是看着该博客进行的coding
https://blog.csdn.net/luoyayun361/article/details/61936032
然后博主告诉了我问题。。。竟然是如此naive的问题。。汗颜。。
我的代码如下:
main.cpp
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
QSurfaceFormat fmt;
fmt.setDepthBufferSize(24);
fmt.setVersion(3, 2);
fmt.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat(fmt);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
engine.addImageProvider(QLatin1String("colors"),new ColorImageProvider);
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
main.qml
Window {
Image{
source: "image://colors/yellow"
}
}
ColorImageProvider.cpp
#include "include/ColorImageProvider.h"
ColorImageProvider::ColorImageProvider():
QQuickImageProvider(QQuickImageProvider::Pixmap){
}
ColorImageProvider::~ColorImageProvider(){
}
QPixmap ColorImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize){
int width = 100;
int height = 50;
if(size)
*size = QSize(width,height);
QPixmap pixmap(requestedSize.width()>0?requestedSize.width():width,
requestedSize.height()>0?requestedSize.height():height);
pixmap.fill(QColor(id).rgba());
return pixmap;
}
ColorImageProvider.h
#ifndef COLORIMAGEPROVIDER
#define COLORIMAGEPROVIDER
#include <QQuickImageProvider>
#include <QImage>
#include <QPixmap>
class ColorImageProvider:public QQuickImageProvider{
public:
ColorImageProvider();
~ColorImageProvider();
QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize);
//QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize);
};
#endif // COLORIMAGEPROVIDER
报错:
其实错误就是我在main.cpp里面,在加载qml文件之后注册了。应该把这两行代码颠倒一下:
感谢大佬帮忙。再次为自己的菜而感到深深的羞愧 - -
-----------------------------------------------END----------------------------------------