Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在QML/QT(Windows下)中设置绝对映像路径时出错_Qt_Qml_Absolute Path - Fatal编程技术网

在QML/QT(Windows下)中设置绝对映像路径时出错

在QML/QT(Windows下)中设置绝对映像路径时出错,qt,qml,absolute-path,Qt,Qml,Absolute Path,在windows下,我无法加载具有QML中绝对文件路径的图像。 每次出现以下错误时: QML映像:无法打开:“文件//d/folder/image1.jpg” 在Ubuntu下,它工作得非常完美 此代码将图像设置为动态: 图像{ id:img x:0 y:25 宽度:227 身高:230 anchors.horizontalCenter:父级.horizontalCenter 来源:“文件:/”+路径 fillMode:Image.preserveApectFit } 在我测试以下命令时,如果

在windows下,我无法加载具有QML中绝对文件路径的图像。 每次出现以下错误时:

QML映像:无法打开:“文件//d/folder/image1.jpg”

在Ubuntu下,它工作得非常完美

此代码将图像设置为动态:

图像{
id:img
x:0
y:25
宽度:227
身高:230
anchors.horizontalCenter:父级.horizontalCenter
来源:“文件:/”+路径
fillMode:Image.preserveApectFit
}
在我测试以下命令时,如果我单击图像:

onclick:{
console.log(路径)
}
然后我得到当前路径:
D:/folder/image1.jpg
是否有windows的工作环境

问候

好的,我找到了解决办法。 我实现了一个<代码> QDeclarativeImageProvider ,它处理C++中的图像路径并返回一个像素映射。 如果您感兴趣:

imageprovider.h
\ifndef图像提供程序\u H
#定义IMAGEPROVIDER_H
#包括
类ImageProvider:public QObject、public QDeclarativeImageProvider
{
Q_对象
公众:
ImageProvider(QDeclarativeImageProvider::ImageType);
~ImageProvider();
QImage requestImage(常量QString&id,QSize*size,常量QSize&requestedSize);
QPixmap requestPixmap(常量QString&id,QSize*size,常量QSize&requestedSize);
};
#endif//IMAGEPROVIDER\u H
imageprovider.cpp
#包括“imageprovider.h”
#包括
#包括
#包括
#包括
ImageProvider::ImageProvider(QDeclarativeImageProvider::ImageType):
QDeclarativeImageProvider(类型){}
ImageProvider::~ImageProvider(){}
QImage ImageProvider::requestImage(常量QString&id,QSize*size,常量QSize&requestedSize)
{
QImage图像(id);
QImage结果;
if(requestedSize.isValid()){
结果=image.scaled(requestedSize,Qt::KeepAspectRatio);
}否则{
结果=图像;
}
*size=result.size();
返回结果;
}
QPixmap ImageProvider::requestPixmap(常量QString&id,QSize*size,常量QSize&requestedSize)
{
QPixmap图像(id);
QPixmap结果;
if(requestedSize.isValid()){
结果=image.scaled(requestedSize,Qt::KeepAspectRatio);
}否则{
结果=图像;
}
*size=result.size();
返回结果;
}
在mainwindow.cpp中注册
view->engine()->addImageProvider(QString(“extern”),imageProvider);
qml文件的片段
图像{
id:img
x:0
y:25
宽度:227
身高:230
anchors.horizontalCenter:父级.horizontalCenter
来源:“image://extern/“+路径
//在windows源中找不到绝对路径:“文件:/”+路径
fillMode:Image.preserveApectFit
}

“file//d/folder/image1.jpg”不是有效的URL。应该是“file:///d:/folder/image1.jpg“

请使用下面的路径
”qrc:///your_imagepath“

谢谢!对我的帮助超过了公认的答案;-)