How to add prefix in QtCreator - qt

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.

Related

Qml works when run but will not load photos in Qt designer

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.

Qt Image from resource file

I'm trying to insert an image to my program via resource file, which is like:
<RCC>
<qresource prefix="/">
<file>green.png</file>
<file>other files</file>
</qresource>
</RCC>
and when I'm trying to load it using QImage or QPixmap, like:
QImage *green = new QImage(":/green.png");
if(green->isNull()) qDebug("null");
I always see that null message, indicating that I'm doing something wrong. One solution may be using absolute path like
"C:\\Users\\user\\Documents\\project\\green.png",
which works of course, but I'd prefer implement it using resource file. Could you please give me some advice?
All this will work if your png files are located in the same folder as .pro, .qrc, and .cpp files of your project.
Usually it is convenient to put all images to special subfolder Resources, for example. Then in .qrc the line will look like:
<file>Resources/green.png</file>
And in .cpp file:
QImage *green = new QImage(":/Resources/green.png");
First of all, you need to create a .qrc file and add image folder to it
(image folder must be contained inside of the project folder)
Right-click on the project file
Add New
Qt
Qt Resource File press Choose and do other steps
after opening .qrc file you must press Add > Add Prefix > change prefix name if you want
again Add > Add File > and choose your images
then go to mainwindow.cpp (in my project ) file and call the images as below code
in my case the icon folder is Playericons
ui->play->setIcon(QIcon(":/Playericons/icons8-pause-30.png"));
Did you remember to run qmake after adding the resource file?

QML + .qrc: Keeping images under another prefix

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!

How to import a resource in Eclipse plugin stylesheet?

To create an Eclipse plugin, I need to import/use some resources into a stylesheet.
All the necessary files reside into the plugin package and are correctly build with no errors into the final .jar.
I have this folder/files in plugin package:
> my.plugin.package.name
> META-INF/
> MANIFEST.MF
> resources/
> css/
> base-stylesheet.css
> win-stylesheet.css
> linux-stylesheet.css
> ...
> images/
> mytexture.png
> ...
> plugin.xml
> ...
in win-stylesheet.css I want to import base-stylesheet.css and use mytexture.png, so into the CSS I do:
#import url("base-stylesheet.css");
#elemId {
background-image: url(../images/mytexture.png);
}
unfortunately, these gives a MalformedURLException, I noticed also that:
using character " or ' or nothing to delimit the url string give the same error;
using #import url("/base-stylesheet.css") or #import url("./base-stylesheet.css") works perfecly when running/debugging the plugin into a second Eclipse instance but silently fails with the same error when the plugin jar is installed in Eclipse;
using #import url("platform:/plugin/my.plugin.id/resources/css/base-stylesheet.css") or #import url("platform://plugin/my.plugin.id/resources/css/base-stylesheet.css") where my.plugin.id is the Bundle-SymbolicName assigned in MANIFEST.MF give the same error both when running/debugging the plugin and when using it after installing the jar.
I suspect that the workspace directory is automatically assigned to platform:/plugin/org.eclipse.platform/.
How can I solve it?
The existing Eclipse stylesheets just use
#import url("base-stylesheet.css");
for css in the same directory.
For resources you specify the location in the applicationCSSResources property in the product extension point properties:
<extension
id="product"
point="org.eclipse.core.runtime.products">
<product
name="%product.name"
application="org.eclipse.e4.ui.workbench.swt.E4Application">
<property
name="applicationCSSResources"
value="platform:/plugin/my.plugin.package.name/images/">
</property>
The blog post "Import CSS files in Eclipse Luna M6" from Lars Vogel seems to imply that the import now works, meaning you need at least an Eclipse 4.4 Luna M6.
Not only that 4.4 M6 release comes with a dark theme for the IDE, it also lists:
CSS includes
CSS files for Eclipse can now include other CSS files via the #import url("platform:/plugin/Bundle-Symbolic-Name/path/file.extension"); statement.
This allows easy reuse of existing style sheets.
For instance (Vogel's post):
In your RCP application, create a /css/default.css file with only one instruction:
#import url(“platform:/plugin/org.eclipse.ui.themes/css/e4-dark.css”);
Add org.eclipse.ui.themes to your RCP product and add the applicationCSS property pointing to platform:/plugin/test/css/default.css in your product extension point.

How to display icon in QMessageBox?

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?

Resources