Qt how to access resources - qt

Qt Creator give the possibility to attach some resource to the project.
I created in the project directory a folder called: Images. inside i have the file splash1.jpg
I created then the Resources file and attached the file as shown in the following figure:
Which is now the correct way to access from code such a file?
I tryed
QPixmap pixmap( "Images/splash1.jpg" );
QPixmap pixmap( "./Images/splash1.jpg" );
but none of them worked.
if i put just ./Images/splash1.jpg work at least if i compile by hand with qmake and make because has the correct path at runtime but no way to make it work within qt creator
Any idea??
Cheers,

Qt5 resources explains all you need. You have to put the colon before the path in the source tree. You also have placed a prefix, so :/Images/Images/splash1.jpg. This will give you a path.
If you need a URL, then use the qrc: scheme.

The correct way to load with Qt resources is: :/Images/Images/splash1.jpg.
What you could also do, is assign an alias to the resource. This way you don't need the .jpg: :/Images/splash

You can use ":/prefix/name of your image".

Related

How to add the path of images in a QML project correctly

I'm going to practice this example and for that I created a Qt Quick Console 2 Application project named Main2 using Qt Creator 4.3.0 on a windows 7 machine.
The code for main.qml looks like this:
and I get the following errors in the Application Output window:
The program has unexpectedly finished.
C:\Users\Abbasi\Documents\QML\Main2\Main2\build-Main2-Desktop_Qt_5_9_0_MinGW_32bit-Debug\debug\Main2.exe crashed.
Starting C:\Users\Abbasi\Documents\QML\Main2\Main2\build-Main2-Desktop_Qt_5_9_0_MinGW_32bit-Debug\debug\Main2.exe...
QML debugging is enabled. Only use this in a safe environment.
What is the problem with the paths or anything please?
The images are in a folder named images on the Desktop. How to move them to a folder under the project, a step-by-step means please?
copy the images folder to your project folder
right click on your project - add new - Qt - resource file - images.qrc
right click on that - add existing files, goto /images and select what you want to add
alternatively, you can directly use the "add existing directory" option to add all files in a directory
Then from QML you simply:
Image { source: "images/whatever.jpg" }
This way the images are neatly packed inside your executable. Which is OK in most cases, unless you have gigabytes of images. It also helps prevent people replacing your stock images with profane versions ;)
Creating a separate resource file for images will do wonders for your build times.
If you are that keen on file system access, be that absolute (please don't do that ever in production) or relative, you will have to prepend a file:// to the path to tell Qt you want the file system rather than internal resources.
Update: As revealed by your main.cpp file, the actual problem is you are using a project template that uses QQmlApplicationEngine. The tutorial you are following dates back to the days before that, and uses QQuickView. The latter can have any QML element as a root, but the former needs to have an ApplicationWindow, which is the reason you are not getting any output. So you need to wrap your existing code in an ApplicationWindow:
ApplicationWindow {
visible: true
width: 640
height: 480
// your code goes here
}
use qrc: with image URL might work
Image { source: "qrc:images/whatever.jpg" }

Java app deployment and installer parameters

I am using javapackager for making installer from .jar. I need to give some params to my installer(splash screen icon, remove version from app name etc), that can't be given directly by javapackager. Instead javapackager has an paramfile option, but I don't know how to use it, and what structure will it have?? Can anyone help me??
it is a java properties file. You can look in https://github.com/Debian/openjfx for the parameter you want to override and then set it in the properties file.

qt how can I reload the resources files

I'm using stylesheet for my new widget. I want to add a reload button just for designing. So I add a stylesheet.txt in Resouces/.../xxx.qrc file as the style sheet to apply for my widget. And I have a QPushButton to trigger setStyleSheet() with a QFile to open the stylesheet.txt. And I want to edit the txt outside the program with defaut editor in system. But I found that the resources files are not refreshed which means when I edit the txt, the txt doesn't reload in the program. Any idea how can I reload the file or any solution, please?
Resources are embedded into the binaries upon compilation. If you edit one of the resource files you'll have to rebuild the application. So, if you want dynamically change styles without recompiling/restarting, just load style-sheet from an external file, but not from resources.
From the Qt Doc:
"The Qt resource system is a platform-independent mechanism for storing binary files in the application's executable. This is useful if your application always needs a certain set of files (icons, translation files, etc.) and you don't want to run the risk of losing the files."
The resources are stored in the binaries, you can only update them if you rebuild your application. Use a other file to load your stylesheet.
General suggestion: do not put resources in .qrc during debug / design. I recommend to use a QDir::setSearchPaths instead:
void Application::setDirs()
{
#ifdef QT_DEBUG
QDir dir( QGuiApplication::applicationDirPath() );
dir.cd( "C:/DotaClient" );
QDir::setSearchPaths( "qml", QStringList( dir.absolutePath() ) );
#else
QDir::setSearchPaths( "qml", QStringList( ":/DotaClient/" ) );
#endif
}
Access:
m_mainView->setSource( QUrl( "qml:Root/Root.qml" ) );
Or something like background-image:url(images:Root/root_bg.png); in QSS.
In this case, file Root.qml will be looked in C:/DotaClient/Root/Root.qml in debug build (with possibility of dynamic reloading), and in :/DotaClient/Root/Root.qml (in resources) in release build.

How can i set application icon in javafx2

javafx set application ico
when i create java project , add jfxrt.jar
it's right
new Image(getClass().getResourceAsStream("../images/customLogo.png"))
but this in javafx project
it's wrong.
how can i get relative path do this in javafx project.
I think that you should take a look on the Image API and this another link where how to put the icon is answered.
If you were generating a jar, you can use
new Image("/path/to/image")
The path to the image starting with "/", will be an absolute path inside the jar. I strongly recommends go to the Image API to check all the options, is really well documented.
Hope it helps
So what is the exception? Where did you create the project (which IDE). As a general rule never use ..-paths when doing look ups although the might work on the filesystem they e.g. break inside jar-files IIRC.

Customise QPushButton with an image

I am using Qt5 and having some problems in setting the back ground image of a QPushButton. I am using Qt Creator and Design to drag and drop the QPushButton. After that I created a source folder and placed an image in that directory and I googled and tried doing
ui->pushButton_play->setStyleSheet(QString::fromUtf8("background-image: url(:/source/play.png);"));
but no success...am I creating the folder in a wrong place?..I created in D:\Qt5_PROJECT_NAME\source or will I make the url(:\\source\\play.png). Nothing is working...please help...
Did you add the images file into .qrc file? A URL start with : has indicated that It will be searched in .qrc file. If .qrc file didn't used, URL should not start with :. Maybe you should access the image file as
ui->pushButton_play->setStyleSheet(QString::fromUtf8("background-image: url(source/play.png);"));
This is The Qt Resource System documentation.

Resources