Problem with dynamically loaded images when using Qt Resource System
-
Okay, then I try to explain it again. Sorry if my first post was too confusing.
All of my images are in the resource file, without exceptions. I provided two code examples in my first post, and in both of them I want to use images from the resource file.
Of course all of the paths look like relative paths, but as you said, the QML engine totally switches to qrc mode and then it looks up everything in the resource file. That's exactly what I want. I leave the relative paths as they are, but transfer the images to QRC and then it still works fine.The strange thing is, that the first one works and the second one does not. When I define URLs as "property url imagepath: ...", then it doesn't work. I don't understand why the QML engine doesn't find these images in the resource file, though they are definitely there...
-
OK, thank now I get it. "dynamically loaded images" got me into thinking that you are trying to include pictures from outside of QRC.
My first bet here is that you should define string properties instead of url properties.
-
Hello,
this code works like a charm:
@import QtQuick 2.4
import QtQuick.Window 2.2Window {
visible: true
property int someInteger: 0
property url imagePath: "qrc:/Chrysanthemum.jpg"
property url imagePath2: "qrc:/Desert.jpg"function getImagePath() { switch (someInteger) { case 0: return imagePath; case 1: return imagePath2; } } Image { source: getImagePath() MouseArea { anchors.fill: parent onClicked: someInteger = someInteger == 0 ? 1 : 0 } }
}@
Tested with QtCreator 3.3.0 and Qt 5.4.0 using MSVC2013.
Both images were added manually into the qrc file.
I can change someInteger and since QML detects that a variable inside the function getImagePath() changed it will reinvoke the method for source:. It updates perfectly and also the qrc path in QML should begin with "qrc:/", since i remember it could work with ":/" too.Looks like QML can work with that so i assume the problem is the part where you add the images into the qrc.
- Could you please try out the code above and tell us your results?
- And is there any console output in your application?
-
Thank you, this code works perfectly for me too. Now that I know that QML can indeed handle this, the problem has to be somewhere else.
In my code, the objects which should display the images are created from the C++ side. I do this by creating a QQmlComponent and then I call the create() function several times. When the qml objects are created, I call setProperty() to set the variables (the one that I called "someInteger" here). I don't think there are any errors in the code, because when I don't use QRC, then there are no problems.
Another strange thing that I mentioned in my first post is, that it works when I open the app in Visual Studio. It even works when I remove all images from the Visual Studio project, so I'm sure the app only uses images from the QRC file. But when I open the executable itself, then it doesn't work.
Do you have more suggestions which could help me solve the problem? Thanks for the help so far!
Edit: Yes I have some console output in my application, for example I print out the source property of the QML images in the moment when they should be displayed on screen and they always have the correct URL. But I can't see them...
-
Hey,
- Which compiler are you using in the QtCreator?
- Which Qt version are you using?
Maybe you are missing a dependency. Have you checked your executeable with a dependency walker? I need a subdirectory called "imageformats" when i am working with Images. In this path i have got all the required image .dll files.
!http://i.imgur.com/fgRKdzH.png(Example Structure)!
My projects can start and run without them but it won't display any images if i don't have this files.
This seems familiar with your problem. -
I didn't use a dependency walker, I just put in all dll files and then removed them step by step, until I had everything that I needed to run the app. The imageformats folder is already included.
I use the newest Qt version 5.4 and the Visual Studio add-in, not Qt Creator.
-
[quote author="CarolaC" date="1422352564"]I think I will try to use my method together with your example. I'll create the QML object from the C++ side, and also set the property someInteger from the C++ side. I'll post again when I have the results :)[/quote]
Alright, go ahead :).
-So the "imageformats" folder is no problem?- -
I tested this and it's almost the same result as in my app. It works in Visual Studio, but not when I start the executable. I did the following:
I created a QQmlComponent from the QML file, then I created a QObject with the create() method. Then I set a timer which triggers the setProperty() method after 10 seconds, to switch the value of the someInteger variable.
When I open the program in Visual Studio, at first I see an image, then after 10 seconds it changes to the other image. But when I open the executable, I see the image, and after 10 seconds the app crashes.
I also checked if some dependency is missing (I put the executable in a new folder together with all dlls, all plugins, all qml files etc.) but the behavior is the same. I can post my code here, maybe the error can be reproduced:@// GameApp is a QGuiApplication
// MainWidget is a QQuickView@@int main(int argc, char argv[])
{
GameApp app (argc, argv);
MainWidget mainWidget = new MainWidget;
app.connectToView (mainWidget);
mainWidget->setResizeMode (QQuickView::SizeRootObjectToView);
mainWidget->setSource (QUrl ("qrc:/qml/Test.qml"));app.prepareTest ();
mainWidget->show ();
app.exec ();
}@@void GameApp::connectToView (MainWidget* mWidget)
{
if (mainWidget == 0)
mainWidget = mWidget;
}@@void GameApp::prepareTest ()
{
QQmlComponent* component = new QQmlComponent (mainWidget->engine (), QUrl ("qml/Test.qml"));
qmlGroup = component->create ();
QQmlProperty::write (qmlGroup, "parent"
, QVariant::fromValue<QObject*> (mainWidget->rootObject ()));
QQmlEngine::setObjectOwnership (qmlGroup, QQmlEngine::CppOwnership);QTimer::singleShot (10000, this, SLOT (displayImage ()));
}void GameApp::displayImage ()
{
qmlGroup->setProperty ("someInteger", 1);
}@Test.qml:
@import QtQuick 2.4
import QtQuick.Window 2.2Item {
visible: true
property int someInteger;
property url imagePath: "qrc:/images/icons/iconFistL.png"
property url imagePath2: "qrc:/images/icons/iconBig5L.png"function getImagePath() { switch (someInteger) { case 0: return imagePath; case 1: return imagePath2; } } Image { source: getImagePath() }
}@
I don't know if this is a bug or maybe I'm doing something wrong. Unfortunately the app has to be ready next week and I don't know if I can spend more time on this issue. If you want to investigate further, then feel free to do so, but if the issue cannot be resolved, then I don't use the Qt Resource System. That would be okay, too ;)
Many thanks to you two for your help so far!
-
Hi,
I thought I'd step into this thread, maybe you can benefit from our learnings..We have also been seeing issues with qrc in our projects, and the default Qt implementation has a couple of disadvantages:
- It is ugly to have to change the qrc:/ scheme in all your images - ideally you would not need to change the scheme when you switch between qrc and non-qrc
- Qrc has problems if the size of all combined images gets bigger than 50MB. Thus ideally you would ship the image assets separately as non-qrc, and use qrc for your qml files (to protect them from outside use). However, you still should have the option to also put the assets into qrc, if exposing your image files is a problem.
That's exactly what we allow to do in V-Play: the QML & JS files are put into qrc files (this also makes sure no one can read your source code) – the assets are put into an own assets folder and are deployed with DEPLOYMENTFOLDERS += assets in the .pro file. You can also use qrc for assets if your prefer, but it has some limitations like the 50MB limit).
That way, you don’t have the size limitation of qrc files (at above ~50MB qrc hangs).
Also, during development working with DEPLOYMENTFOLDERS is better on Desktop, because you can just re-run your application and dont need to recompile.What we did additionally, is to abstract the file access, i.e. you dont need a prefix assets:/… for your images, or mix up your code with any qrc:/… prefixes.
You can have a look at some .pro files of our games by downloading the "V-Play SDK":http://v-play.net and then check out the <QtSDK>/examples/V-Play/demos directory.
I’ve also posted some comments about the qrc approach set as default since Qt Creator 3.1 on the Qt Blog, maybe that’s interesting to the pros and cons of it (and why I think it’s bad..):
http://blog.qt.digia.com/blog/2014/03/04/qt-creator-3-1-beta-released/#comment-1193034Hope that helps,
Chris