Qt Image from resource file - qt

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?

Related

No headers or source files in QT creator

I'm having a problem with creating new header and source files in QT. I can only add new files to the build, it's grayed for the "main" project as you can see below.
I can create them specifically for the build and they only appear once in the documents area. There is no header tree branch. After I close qt I have to create new ones. I want them to appear in the tree and be connected with the main project. I have "hide source and headers files" option off.
I've obviously tried creating them the way it was shown on the yt. I've searched through settings but didn't found anything I could use.
You will have to add the files manually in CMake.
Go to File>New File and select what type of file you want. Typically it will be a source, header, or both (class).
Edit the CMakeLists.txt file:
qt_add_executable(myapp
myapp.qrc
main.cpp
new_file.cpp
new_file.h
)
Resources like .qml files and things like images (*.png *.jpg) are added in the myapp.qrc 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.

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.

How can files and classes be renamed in Qt Creator?

How do I rename files and classes (declaration, implementation and uses) in Qt Creator 2.0?
I can't find such feature in there.
There is no such feature in Qt Creator :(
This is what you can do:
To rename files:
close your project
rename files in Windows Explorer or some other tool
open project
update *.pro file, list *.h files in headers, *.CPP files in sources, *.ui files in forms
update all *.ccp files in the #include section to include proper files
To change class names you can use the Ctrl + Shift + R feature that changes class names in header files and source files.
If you also want to rename a form and along with its class, you have to edit your name.ui file and change the widget name and class to the new corresponding value.
You also need to rename some things in your name.h file.
namespace Ui {
class NewValueHere;
}
private:
Ui::NewValueHere* mUI;
In your name.cpp file you also have to adjust the call of creating your mUI.
mUI(new Ui::NewValueHere)
Personnally I needed to do something more.
I renamed my class "Form" to "MainWindow", and I had to go in the directory "build-yourproject...-Debug\ui_mainwindow.h" and replace every "Form" with "MainWindow" (Ctrl+f) and then it worked.
EDIT: This was a manual solution that changes a file that is re-loaded at every build, so this change is automatically lost. The right solution is to go in Qt Creator and in the upper-right corner, under the name "Object", you should have the name of your class, that is what you need to change.

How do i change the index.html flex output to admin.anotherExtension?

i need to change the automatically generated index.html flex output to admin.anotherExtension. I understand that if i change the index.template.html the changes in html level will be preserve when the file is generated, however i need to change the name of the file also, and the extension.
Thank you! (using flashBuilder4)
In the html-template directory. Create a new template file named something like this:
${application}${build_suffix}.template.cfm
The next time you build your project, you'll get a file with the extension .cfm that is the same name as your application. You can use whatever extension of your choosing. My source
If you create your own file in the html-template directory (say: index.cfm ), I'm pretty sure that file will be copied to your build directory, but not changed.
Keep in mind all your changes will be overwritten if you change the Flex SDK on the project. Back them up somewhere.

Resources