QML downgrade from 6 to 5
Categories:
2 分钟阅读
so many weird tricks that matter in Qt5 QML
1. the keyword “Window” is unrecongnized
You must type this line in the top of your qml file
import QtQuick.Window 2.3
or the keyword “Window” will be unrecongnized
2. all imported system libs will have their versions
//this is wrong
import QtQuick.Window
//this is correct
import QtQuick.Window 2.3
3. the way of the organization of qml in each project
qt5 use qmldir way to organize every qml module.
in qt6 qmldir is not created by coder it utlizes CMakelists.txt way to manage all qml files
4. qml individual module utilizes the folder name as its module name
e.g., module folder: folder_A folder content: a.qml b.qml qmldir the content of the qmldir:
module moduleA
a 1.0 a.qml
b 1.0 b.qml
now how to import in another certain c.qml file? the answer is
import folder_A 1.0
OMG, in qt6 everything has been organized in sub CMakelists.txt
### This file is automatically generated by Qt Design Studio.
### Do not change
###########################################################################################
# 9th
set_source_files_properties(SingletonModule/Exposes.qml PROPERTIES
QT_QML_SINGLETON_TYPE TRUE
)
qt6_add_qml_module(SingletonModuleNamesssss
URI "SingletonModuleName"
VERSION 1.0
QML_FILES
SingletonFolder/a.qml
)
qt_add_library(content STATIC)
qt6_add_qml_module(content
URI "content"
VERSION 1.0
RESOURCE_PREFIX "/qt/qml"
QML_FILES
main.qml
RESOURCES
fonts/fonts.txt
../images/icons/g_icon.jpg
../images/icons/h_svg.svg
../images/icons/k_svg.svg
)
now how to import my singleton module which created by myself?
import SingletonModuleName 1.0
the URI is the key string so direct and simple without any confusion
but qt5, the mechanism is the import will cause engine to search the target folder with that exact name as we write with import keyword.
5. all qml files be put under qrc file defaultly
of course, you can choose not to manage them under qrc but this way is the default option
6. you must register the data type(s) if you wanna share data between cpp and qml
qRegisterMetaType<data_type_a>("data_type_a");