qt how can I reload the resources files - qt

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.

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

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" }

How record Qt Widgets StyleSheet in a sepatated file

Is it possible to get the stylesheet from a separated file for a Qt Widgets application, like .CSS does to .HTML ?
Yes, it is possible. You may load any file with QFile and use setStyleSheet method in your application or any widget. by #SaZ

Qt how to access resources

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".

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.

Resources