Rebuilding again and again to view the imported QML file changes - c++

I have imported a qml file say A in other qml say B. Whenever I make some changes in A , it doesn't reflect in the Application. I have to again clean and rebuild to view the changes i made, which is very time-consuming. Is there an easier and efficient way of doing this, so time could be saved .
Or another way of saying this is :
qrc files are not added to Makefile dependencies in debug_and_release mode
example
A.qml
Rectangle {
id:xyz
Button
{
id: ButtonA
}
}
B.qml
Rectangle{
A {
id:abc
}
}
I am using qt 4, qtquick 1.0.

You can make two independent qrc files, one for debug which is only includes resources like sounds, images, fonts and ... and another qrc for release which includes qml files too.
Then you can use point to the qml files relatively with your current working directory (could be set in Build & Run pane) without need to include them in qrc which requires time consuming compilation and linkage.
Also be aware, the root qml file should be pointed by relative (or absolute path) from C++ side, and no other changes are required to other qml files and they works perfectly on both release and debug mode
In your pro file :
debug {
RESOURCES = application_debug.qrc
}
!debug {
RESOURCES = application_release.qrc
}

You can install the new qt creator, it will solve the problem. Worked for me. I installed QT creator 4.8.7.

Related

How can I load an image to QPixmap without specifying the full path?

I'm using Qt Creator on Windows and trying to add an image to an icon.
The only way I can get it to work is by specifying the full path:
QPixmap newIcon("C:/Users/slipn/Documents/qt/projetos/exemplo/new.png");
newToolBarAction = toolbar->addAction(QIcon(newIcon), "New File");
I tried adding to the project as shown below:
The image and the code files are in the same directory. I have no clue of what path I should use to make it work. Any hints?
The problem you need to solve, is that your source path and the path where you build or maybe later even install your application are not the same. You need to bundle your image resource with your application, either by copying & installing it to your build/install, or by including it directly in your application binary.
The latter is your easiest bet with qmake. Installing files with qmake is (relatively) easy, but making your build result work without a separate make install step is a different thing.
Your easiest bet to solve both "running your build result directly" and "running the installed application" is to use the Qt Resource System.
Create a .qrc file with File > New File > Files and Classes > Qt > Qt Resource File, add a "prefix" (some name), add your image file there, and then use QPixmap newIcon(":/yourprefix/new.png") to access it in your application.

How to include qrc data into Qt-based binary that was compiled using CMake

I have a big Qt + third-party-library project which I try to compile into a binary so that to test the program in another machine. I was able to make the binary run (found all the necessary *.dlls and plugins), however, I cannot figure out how to include a *.qrc data with icons for my program. For the moment, the binary cannot load any icons, so I have just buttons with text.
The code structure of my program is as follows:
root_folder
CMakeLists.txt
program_folder
main.cpp
CMakeLists.txt
data_folder / Data.qrc and set of icons in svg format
other .cpp and .h files
lib_folder1
lib_folder2
...
This is how Data.qrc file looks inside:
RCC>
<qresource prefix="/">
<file>file-name.svg</file>
...
This is how I add the resources to my program, inside the CMakeLists.txt of the program_folder:
qt5_add_resources(IMG_RSC_ADDED data_folder/Data.qrc )
add_executable(${PROJECT_NAME} ${PROJECT_SRCS} ${IMG_RSC_ADDED} )
Inside one of the .cpp files of program_folder I load the icons:
static QIcon icon(QPixmap(":/file-name.svg"));
For the icons, I have a class Data{}; and inside that class I have a set of methods, e.g., static const QIcon& fileIcon();. And in the main code, when I need to use the icon, I call it this way: Data::fileIcon().
It works when I compile and run from the source.
I prepared the binary distribution of my program, and this it how it is structured inside some root folder:
imageformats
qsvg.dll
platforms
qwindows.dll
my_program.exe
Qt5Core.dll
other Qt and 3rd-party-lib dlls
My question is how and where do I put the data files? I tried different locations, e.g., inside the main folder, inside created Data folder. And I simply copied all the data I had (svg file, qrc file), but the binary still cannot see the resources. How to solve it? Or, what is the common practice?
Note, I am using CMake (not QMake) to compile my binary. I am using Qt-5.4, on Windows 7. Let me know if my question lacks details, I will add them. Thanks!
The following command qt5_add_resources(IMG_RSC_ADDED data_folder/Data.qrc) makes source files with binary data generate from each resource from the provied qrc files(Data.qrc in this case) and the list of the resulting files is stored in the IMG_RSC_ADDED variable. Provided this variable is passed to add_executable all the resources get embedded in the resulting binary.
Next, to the actual problem: the problem is not with the distribution but with the code itself. You create and initialize QIcons in the static context and use QPixmap for it but QPixmap requires QApplication up and running which is not the case with the static initialization context. So the proper fix is to move the initialization of the QIcons to some local context(like class ctor or the main function)

QT Resource File - Images Not Showing When Running

I just upgraded to QT Creator 5.3 and created a brand new QT Widgets Application project and am using the Microsoft VC++ compiler. All I have is a resource file with "logo.png" added (which opens in QT if I double click it), and a label that I am trying to set the background image for. The problem is that no matter what I do I cannot get the image to show up when the program is running. The only way I can get it to show the image is by not using the resource file and instead map directly to the file (ex. "c:/blah/blah/logo.png")
Also, if I set the background image in the UI designer, the background shows up in the IDE but NOT when I run the program. I have tried probably 20+ variations of the code, including resource file aliases as well as adding the files to the project directly, and nothing seems to work.
I'm not sure if there is a step I am missing, or if perhaps there is something that I have to do to get the project to compile the image.
Resources.qrc
<RCC>
<qresource prefix="/">
<file>logo.png</file>
</qresource>
</RCC>
loginform.cpp
QPixmap pixmap = QPixmap (":/logo.png");
ui->label->setPixmap( QPixmap(pixmap));
Project.pro
OTHER_FILES += \
logo.png
RESOURCES += \
Resources.qrc
After countless hours of trial, error, and mostly frustration, I have been able to resolve this issue by simply running qmake.
To do this I just right clicked on the project and clicked "Run qmake" and suddenly the image errors (as well as a few others) were resolved!

Create folders/subfolders in an qt creator project

In my Qt Project, I want to create some subfolders like Audio or Video.
Something like this:
Project\Media
Project\Media\Audio
Project\Media\Audio\Video
How I can do this?
Is there an way to add a new folder in Qt Creator? Or do I need to create this manually via the explorer?
Currently the only way is to add or create file that is inside of the directory - then the directory will be also created in Projects view of the QtCreator. This is not really that bad, since you can create directory while adding a new class file to the project. When the file is added to the project, also all its parent directories will be reflected in the Projects view.
In qt you use mkdir
bool QDir::mkdir ( const QString & dirName ) const
which will create a subdirectory called (variable dirName)
You could also use cmake which allows you to generate makefiles for every platform you need, itsteand of writing them manually. See code examples here

Why is SDL not loading my images when main application is accessed via shortcut?

I have recently created a program using C++ and the SDL library. Originally, all the images and DLLs were in the same folder as the main application. I wanted to make the main application easier to find, so I instead moved all the images into their own folder and modified my source code to find them in the correct folder. So far, so good. The main application still remains in the same folder as the DLLs.
I created a shortcut to the main application and put that it's own folder. I changed the target to %windir%\system32\cmd.exe /q/c start "" ""%SystemRoot%\explorer.exe %UserProfile%\Desktop\Cupcake Corner Build 2.0 (exe test)\dependecies\Cupcake Corner.exe" in an attempt to make a universal shortcut, so that the shortcut would work for any user I send the files to.
After all said and done, I've ran into a problem. If I open the main application itself, it will load the images and work perfectly. If I try to the main application via its shortcut, the window will open correctly and display the correct title, although none of the images will load. I tried moving some files around to see if the shortcut would see them if I put the resource folder elsewhere, but so far no luck.
images here -> C:\Users\Admin\Desktop\Cupcake Corner Build 2.0\dependecies\resources
main app/dlls here -> C:\Users\Admin\Desktop\Cupcake Corner Build 2.0\dependecies\
shortcut here -> C:\Users\Admin\Desktop\Cupcake Corner Build 2.0\
I am using Microsoft Visual Studio Express 2010. I think it has something to do with the way I have my project directory set in project options, but I'm honestly baffled at this point. Can anyone point me in the right direction?
Generally, when dealing with files in an application (that the application knows internally, at least), you will want to have absolute paths.
The best way to solve this is by having a (set of) function(s) that add the absolute path (found by GetModuleFilename(0, ...) or from some configuration for example in the registry)
std::string myfiles_root_path = FindMyFilesRootPath();
std::string MakeFullPath(const std::string &filename)
{
return myfiles_root_path + filename;
}
The function FindMyFilesRootPath() uses one of the methods above to find the appropriate path, and filename is the name of a file local to your application.