Automated removal of unused QML imports - qt

I'm working with a pretty large Qt/QML code base, and I am looking for way to detect and remove all unused QML imports. How can this be done? Is there some way to automate it?
I believe that removing unused QML imports is a good way to keep the codebase clean. Also, according the QML Coding Guidelines, it will improve loading performance:
Imports take time in QML. And If you are developing for a device with low system specifications, then you will want to optimize as much as possible. In that case, try to minimize the number of imports you use in your QML file.

You could parse every single QML file and search for QML dependencies in the code, then remove every other dependency copied by winqtqmldeploy.

After almost two years, I've found a conventional way to do it, provided by Qt via their tool qmlLint :
You may now disable warnings on a per category basis, for example by
default qmllint will inform you about unused imports:
import QtQuick
import QtQuick3D // Info: DEM.qml:2:1: Unused
import at DEM.qml:2:1
Text {
id: textElement
text: "Hello world!"
anchors.centerIn: parent
}
If you wish, you can now either suppress this information by passing
--unused-imports disable. Or you can turn this informational message into a full blown warning by using --unused-imports warning. In that
case, as with all warnings, the unused import warning will make
qmllint return an exit code of 1 ; that will cause failure in any CI
or pre-commit hooks that use the tool. See the output of qmllint
--help for the complete list of warning categories.
Source : https://www.qt.io/blog/whats-new-in-qml-tooling-in-qt-6.2

Related

QML Module not found with registered types

I have the following Issue:
In main.qml I get these errors. Although I can use these types perfectly in the code. It looks like it is just an intellisense issue.
These types are registered in main.cpp:
Thse classes are defined in the include folder:
My folder structure looks like this:
Do I have to modify QML_IMPORT_PATH in the pro file? I added src and include folder but it does not work:
QML_IMPORT_PATH += src
QML_IMPORT_PATH += include
The code itself runs fine. It is just an Intellisense issue.
I assume this is simply a Qt Creator bug. Take a look at this one. qmlRegisterSingletonInstance was added to the Qt library in version Qt 5.14. Even though Qt Creator 4.13.3 was built with Qt 5.15.2, the QML code model it uses has apparently still not been updated.
You need to run this code. QtCreator is notorious for flagging errors that don't exist or won't exist. It flags header files for .ui files because you haven't run a build yet so they haven't been generated. Many developers paint their UI files then do a fake build just to generate those files so QtCreator shuts up.
The other thing you need to do is provide the full source code for one of those classes. (I will assume they all have the same issue.)
The example Qt gives here isn't a good one. You should never be able to "new" a Singleton. The constructor should be protected and the Instance() method should construct one and only one if the internal pointer is null.
Do you actually have a method named get() in each of those classes? Does it actually return a pointer? Many return a reference, that is why I'm asking. If we overlook the glaring error of being able to "new" a Singleton, there is one good thing in this example.
QScopedPointer<SingletonTypeExample> example(new SingletonTypeExample);
They used a QScopedPointer to the class.
qmlRegisterSingletonInstance("Qt.example.qobjectSingleton", 1, 0, "MyApi", example.get());
Once you actually build you will have all of the MOC information where it needs to be. This may well make QtCreator happy. Honestly, I've stopped looking at what QtCreator flags anymore because there are so many false alarms.

How to find the source of a recursive rearrange in QML

I updated my QML application from Qt 5.12 to Qt 5.15.
My application loads its qml sources using the following code:
auto* engine = new QQmlApplicationEngine(this);
...
engine->load(QUrl("qrc:/main.qml"));
When engine->load is called, I now get the following warning, which was not there with Qt 5.12:
Qt Quick Layouts: Detected recursive rearrange. Aborting after two
iterations.
How can I find the source of this warning so I can fix my code?
Edit:
After two downvotes, I would like to clarify the intent of my question.
I have a very large application which is loading a big tree of qml files, with main.qml being the main Window. The warning that I posted comes from the Application output pane, without any hint to a source file location that caused the warning.
I am looking for a way to find the source file location that caused this kind of warning. I believe it is reasonable to ask how to achieve that, and I believe that this is a generic problem that will come up for many people who update their qml code to Qt 5.15. It's in the nature of such a issue that a self-contained example (like asked for in the comments) cannot be provided.
It's a totally reasonable ask - the warning is ambiguous so you'd have to post the entire codebase to get a minimum viable. Afaik there is no reasonable way to locate the offending bits programmatically but look for Layout components (RowLayout, ColumnLayout, GridLayout) nested inside the same kind of Layout component; these are the usual offenders. For instance:
ColumnLayout {
ColumnLayout {
id: childColumnLayout
// this is generally the cause of your grief
// changing the the child ColumnLayout to a Column usually fixes it for me
}
}

How can I measure the time of imports in QML file?

I want to profile my QML and measure the time taken by all the imports of one given QML file.
I know there is a debug timer in QtQuick. But I don't see how I can profile the import part, not even the loading part.
Is there a way to measure that?
You can probably use the QML Profiler for this. In particular, you should take a look at the Compiling and Creating event categories:
https://doc.qt.io/qtcreator/creator-qml-performance-monitor.html#understanding-the-data

How can I find syntax errors in QML files?

I'm doing development for Blackberry 10 using Cascades, which includes QT and QML. I find that I sometimes make mistakes in my QML file, but they don't get picked up at compilation time. How can I check whether I've made a syntax error, or mis-named a function call, or other typical errors?
QML is a dynamic language that is evaluated at Runtime. There is no compilation step and due to the nature of javascript and the dynamic nature of the global context there is no way for it to tell if what you are writing is correct/incorrect until it is evaluated. QtCreator can help with some of the QML errors you will find, but there is unfortunately no good way to get syntax errors about your javascript until it is evaluated and it explodes.
Personally, I have found good usage of the debugger to be the key to making these sort of fixes easy.
tldr; Keep your javascript clean and to a minimum there is no compile time checking.
open terminal in IDE connect your device or emulator using blackberry-SSH after connecting enter slog2info it show syntax and all typical error JavaScript with description and line NO.
If there are any mistakes it will show those lines in RED marks. It is dynamically checks there is no need to worry about compile.
If you done wrong you will not see the DESIGN CONSOLE correctly.

Linking AS code to symbols defined in an external SWC?

(apologies ahead of time, I only really know Flash; my Flex experience is basically nil. There may be a very standard and obvious workflow solution that Flex people know about)
I have a number of UI elements that are graphically quite complex (they're not components, they're just Sprites). Since it takes a long time to compile them, I've been trying to move them into an external .swc. However, I want to associate some code with these classes, but I don't want to have to recompile the graphical assets every time I make a code change.
At the moment I have it set up like this: UI elements are created in a separate FLA and exported to a SWC. In my primary FLA, I have actionscript classes that extend each of the graphical assets in the SWC. For example:
external.swc:
(some symbol defined in the Library and exported for actionscript in frame 1)
class: com.foo.WidgetGraphic
base: flash.display.Sprite
main.fla:
Widget.as:
package com.foo {
public class Widget extends WidgetGraphic {
...
}
}
This works, but is time-consuming and prone to error. I'd rather be able to avoid having to inherit from each graphical asset, and just define them directly. Is there a better way to do what I'm trying to accomplish?
Note: the main concern here is compile time. I don't have any movies or audio or fonts, just a lot of vector art assets that appear to be slowing down my compilation time significantly. When I'm debugging I'm only making code changes, and would rather not have to keep recompiling the art...
Try to compile your project with Flex SDK. Export your graphics, fonts, music etc. to swc, and compile your project with Flex SDK. If you're on Windows, check out FlashDevelop, it will helps you to start building projects with Flex SDK.

Resources