My problem has been similar to that discussed in another stackoverflow discussion and I could get my code working that way. However, I'm not completely satisfied with that solution. Initially, I had my .qml-file under one prefix ("/") and my images under another ("/images"). Without abandoning this separation, I don't get the program running.
Is there any (simple) way to use different prefixes in a QML project with .qrc resource file?
If you want to group the files by prefix based on the actual folder name without lengthening the reference name then why not use the alias keyword?
<RCC>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
<qresource prefix="/images">
<file alias="foo.png">images/foo.png</file>
</qresource>
</RCC>
This will remove the extra indirection that was causing confusion i.e. you can access it with :/images/foo.png instead of :/images/images/foo.png
An explanation and further examples are available in the Qt docs (search for "alias"): http://doc.qt.io/qt-5/resources.html
Instead of asking, I should have gone drinking a coffee or do some sports. It's an embarrassing beginner's problem. Still, there might be others like me...
My QtQuick application consisted of essentially of a C++ source file main.cpp, a resource file qml.qrc and an image foo.png.
Source file (the shown code is generated automatically by QtCreator):
//main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
return app.exec();
}
Resource file with additional prefix for images:
//qml.qrc
<RCC>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
<qresource prefix="/images">
<file>images/foo.png</file>
</qresource>
</RCC>
A qml file, where I want to import an image:
//main.qml
import QtQuick 2.2
import QtQuick.Window 2.0
Window {
visible: true
Image {
source: ???
}
}
My problem was that I didn't know what to write instead of ??? in the .qml file. To import the graphic you need to write "/images/images/foo.png", but my mind revolted against the idea of writing /images twice.
Thanks.
Instead of ???, you must put the path "qrc:/images/images/foo.png"
Example:
//main.qml
import QtQuick 2.2
import QtQuick.Window 2.0
Window {
visible: true
Image {
source: "qrc:/images/images/foo.png"
}
}
In your case, instead of writing "images" twice as your source path, you could have defined the file in your resource file as follows:
<file alias="foo.png">images/foo.png</file>
Then you are allowed to use "images" only once.
Window {
visible: true
Image {
source: "qrc:/images/foo.png"
}
}
See the documentation!
Related
Currently I've Custom Components in / prefix. I want to store all Custom Components in another prefix. Here's how I tried to create a prefix.
but nothing appears under qml.qrc
Open your qml.qrc file in a text editor and you should see something like this:
<RCC>
<qresource prefix="/">
<file>Components/PathButton.qml</file>
...
Change that to this:
<RCC>
<qresource prefix="/MyPrefix/">
<file>Components/PathButton.qml</file>
...
Now you will have to use import "MyPrefix/Components in your qml file.
When working on a small qml project I found this behavior and I am not sure if I am doing anything wrong or if this is just how it is.
I created a simple test file Test.qml along with the main.qml and main.cpp
import QtQuick 2.0
Item {
width: 212
height: 212
Image {
id: image
x: 0
y: 22
width: 212
height: 168
source: "images/PaperSpeed.png"
fillMode: Image.PreserveAspectFit
}
}
The image linked in source above
shows the actual image in designer
but fails to show the image when running
I go ahead and change the source to the qrc URL:
source: "qrc:/img/images/PaperSpeed.png"
but now it fails to show up in designer
and instead shows up when running
What I find even weirder The image does show up in designer when on the Test.qml file but does not show up in designer when on the main.qml file for both source cases
my .pro file:
QT += quick
CONFIG += c++11
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp
RESOURCES += qml.qrc
QML_IMPORT_PATH =
QML_DESIGNER_IMPORT_PATH =
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
and the qrc file:
<RCC>
<qresource prefix="/">
<file>Thermostat.qml</file>
<file>main.qml</file>
<file>Test.qml</file>
</qresource>
<qresource prefix="/img">
<file>images/PaperSpeed.png</file>
</qresource>
</RCC>
This did seem to happen out of no where when I tried to import a new module like QtQuickContols eventually got rid of it but the problem persisted.
I have tried messing around with the QML emulation layer and moving the files around within the qrc. Anyone have any ideas as to what I can try or what may be going on?
Sorry in advance for any formatting issue!
I had same issue. It solves if i add an image to window.
Check the exaple below. I have same result if i remove test image from window.
For anyone still looking at this, what worked for me is separating the UI elements into a .ui.qml and the backend logic into a .qml file. This does a better job at loading UI elements and pictures when the .ui.qml file is opened in designer.
I'm pretty new to QtCreator, my issue is that I'm not able to see QML Custom Components.
For example if you import QtQuick.Controls 1.5 you see under QML types:
- Qt Quick - Controls.
So far I have created my personal set of components, here is the folder structure:
the components qmldir is done in that way:
# qmldir
module components
Header 1.0 Header.qml
Footer 1.0 Footer.qml
CentralPage 1.0 CentralPage.qml
GenericButton 1.0 GenericButton.qml
ProgressBarCustom 1.0 ProgressBarCustom.qml
than for example in the LandingPageFrom.ui.qml
import QtQuick 2.6
import QtQuick.Controls 1.5
import QtQuick.Layouts 1.3
import components 1.0 as Components
Components.CentralPage {
pageName: "landingPage"
id: landingPage
width: 800
height: 1056
property alias aButton: aButton
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
}
And is compiling and showing fine but inside the Designer nothing is showing as aspected, I only see .qml that are in the same directory but not the ones in components directory:
this is what I have done in the .pro and in the main.cpp
QML_IMPORT_PATH += $$PWD/resources/common/ui
QML_DESIGNER_IMPORT_PATH += resources/common/ui
main.cpp
QQmlApplicationEngine engine;
engine.addImportPath("qrc:///ui");
engine.load(QUrl("qrc:///ui/main.qml"));
What is strange is that only the designer is not able to see the components, but the QML code is able, what I'm missing here?
Not sure if you already solved your issue, but try steps 4 and 5 here:
http://doc.qt.io/qtcreator/creator-qml-modules-with-plugins.html
That made it work for me, although I had some issues with the metainfo file. You can just use this guys metainfo file as an example(or look at the one the Qt docs referenced in the instructions above): https://forum.qt.io/topic/56207/how-to-load-custom-qml-controls-into-the-qml-designer/2
Just make sure you put the designer folder where your module qmldir is located and the metainfo file goes in there. Probably named components.metainfo judging by the module name, not sure if that matters.
When I import qml from qrc (which place in different directory from ) it's compile and work fine.
But when Qt Creator don't recognize imported component and don't hightlight this.
This's code:
import QtQuick 2.0
import "qrc:/qml_libs/ApplicationContainer"
Item
{
id: root_object
width: 300
height: 200
ApplicationContainer {
width : parent.width
height: parent.height
}
}
#Aleus, your way of doing thing is little bit tricky. I can't find any information of such kind importing in official documentation (please, take a look at Importing statements in Qt 5.2, QML. Also, take a look at QML Best Practices: Coding Conventions
First of all, check weather your file ApplicationContainer really has no extension (I mean .qml)
To make QtCreator recognize imported component do next things:
Add your qml file to .pro file in section OTHER_FILES like this:
OTHER_FILES += qml_libs/ApplicationContainer
AND/OR place statement for importing whole directory to your qml file, like:
import "qml_libs" as MyLibs
...
MyLibs.ApplicationContainer { ... }
Hope it helps!
I have an about box that I'm trying to display an icon inside.
Here is my code:
QMessageBox about_box(this);
about_box.setText("...");
about_box.setIconPixmap(QPixmap("qrc:/images/logo.png"));
about_box.setParent(this);
about_box.exec();
Here is my resource file:
<RCC>
<qresource prefix="/images">
<file>logo.png</file>
</qresource>
</RCC>
You don't need the qrc prefix:
about_box.setIconPixmap(QPixmap(":/images/logo.png"));
You will need this function
EDIT: I didn't see that the OP had already used this.
Are you sure you're running qmake (and thus rcc) when compiling?