issues with using qt_policy(SET QTP0001 NEW)
-
Hi all -
I'm trying to eliminate a build warning by using the CMake directive in my title. I've gone through the docs, and I see that this changes the default resource prefix for my project, but I'm not sure what that means.
When I run my program, I get the error: qrc:/nga_demo/main.qml: No such file or directory. I've tried setting my RESOURCE_PREFIX argument in my qt_add_qml_module() command, but that didn't help.
So, a few questions about this:
-
I always get confused between paths and prefixes. Am I expected to actually create a /qt/qml path and put all my .qml files in it? If the answer is "no," would it be a good idea anyway?
-
With my current folder structure, I set my main URL to:
const QUrl url(u"qrc:/nga_demo/main.qml"_qs);
Does this need to change?
- I thought that using qt_add_qml_module() and listing all my .qml files in it obviated the need to list all my .qml files in the qml.qrc file...true or false?
Thanks for any enlightenment in this area...
-
-
@mzimmers said in issues with using qt_policy(SET QTP0001 NEW):
I see that this changes the default resource prefix for my project, but I'm not sure what that means.
When I run my program, I get the error: qrc:/nga_demo/main.qml: No such file or directory.The new policy means that the new default path for your file is now
qrc:/qt/qml/nga_demo/main.qml
I've tried setting my RESOURCE_PREFIX argument in my qt_add_qml_module() command, but that didn't help.
If you want to keep your old path, you need to use
RESOURCE_PREFIX /
. (But in most cases, I would just change to the new path and move on)- I always get confused between paths and prefixes. Am I expected to actually create a /qt/qml path and put all my .qml files in it? If the answer is "no," would it be a good idea anyway?
No, and no. The idea of the policy is to make everything automatic, not to add more work for developers.
- With my current folder structure, I set my main URL to:
const QUrl url(u"qrc:/nga_demo/main.qml"_qs);
Does this need to change?
See my 1st two responses above.
- I thought that using qt_add_qml_module() and listing all my .qml files in it obviated the need to list all my .qml files in the qml.qrc file...true or false?
True. qt_add_qml_module() puts things in the qrc for you, so you shouldn't do it manually.
-
@JKSH OK, so I've made the following changes to my project:
- QTP0001 is set to NEW in my CMakeLists.txt file.
- main.cpp now has this symbol:
const QUrl url(u"qrc:/qt/qml/nga_demo/main.qml"_qs);
- I've emptied out my qml.qrc file.
At runtime, I get an error: qrc:/qt/qml/functions.js: No such file or directory
The import for this file occurs in a module that resides in a subdirectory, so the import statement reads:
import "../functions.js" as Fn
So, what did I do wrong, or what am I missing here?
Thanks...
-
@mzimmers said in issues with using qt_policy(SET QTP0001 NEW):
So, what did I do wrong, or what am I missing here?
To answer that, I'll need to see how you added function.js to your CMakeLists.txt.
But anyway, you can inspect your resources directly by adding these lines to main() and #include-ing the relevant headers:
QDirIterator qrc(":", QDirIterator::Subdirectories); while(qrc.hasNext()) qDebug() << qrc.next();
-
@JKSH I add function.js as follows:
qt_add_qml_module(appnga_demo URI nga_demo VERSION 1.0 QML_FILES main.qml functions.js Activitydrawer.qml ...
I put the code you suggested in my main.cpp, and got a ton of output, but this line was included:
":/qt/qml/nga_demo/functions.js"
EDIT: whatever is wrong with my attempted use of Functions.js (now renamed with an uppercase first letter) is also wrong with my Colors singleton, registered accordingly:
qmlRegisterSingletonType( QUrl( "qrc:/Colors.qml" ), "colors.stylesheet", 1, 0, "Colors" );
and starting with:
pragma Singleton import QtQuick 2.0 import QtQml QtObject { property color callToAction: "#6845da" property color accent: "#4147f0" property color accentIllust: "#e3a26b"
There is a section at the end of this page that discusses singletons:
The API so far was used to create new objects, and thus it’s unsuitable for QML singletons.
if I understand this correctly, I guess I need to reimplement all of my singletons as well? -
@mzimmers said in issues with using qt_policy(SET QTP0001 NEW):
":/qt/qml/nga_demo/functions.js"
There we go. You tried to import
qrc:/qt/qml/functions.js
but you need to importqrc:/qt/qml/nga_demo/functions.js
. So adjust your import statement accordingly.qmlRegisterSingletonType( QUrl( "qrc:/Colors.qml" ), "colors.stylesheet", 1, 0, "Colors" );
Do not mix qt_add_qml_module() with qmlRegisterSingletonType(). Let CMake do the automatic registration; get rid of qmlRegisterSingletonType()
There is a section at the end of this page that discusses singletons:
The API so far was used to create new objects, and thus it’s unsuitable for QML singletons.
if I understand this correctly, I guess I need to reimplement all of my singletons as well?No, that section discusses singletons implemented in C++. Your implementation is fine, you just need
set_source_files_properties(Colors.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE )
See https://doc.qt.io/qt-6/qt-target-qml-sources.html#qt-target-qml-sources-example for an example.
-
@JKSH OK, this is making progress. I added this to my CMakeLists.txt file:
set_source_files_properties(Color.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE )
And removed the qmlRegisterSingletonType() line. Now, this line in my Main.qml:
import Colors
Gives an error "QML module not found." And Colors.qml is still in my qt_add_qml_module() statement. What might I have missed here?
Thanks...
-
@mzimmers said in issues with using qt_policy(SET QTP0001 NEW):
import Colors
You must import the URI as specified in the qt_add_qml_module() function that contains Colors.qml
-
@JKSH said in issues with using qt_policy(SET QTP0001 NEW):
You must import the URI as specified in the qt_add_qml_module() function that contains Colors.qml
My function reads as:
qt_add_qml_module(appnga_demo URI nga_demo VERSION 1.0 QML_FILES (other files) Colors.qml (other files)
I don't know what you mean by "import the URI" -- do I need to prefix my import statement (I've tried a few combinations, but none worked)?
import "nga_demo/Colors.qml" // runtime error: no such directory import "qrc:/nga_demo/Colors.qml" // same error as above import "qrc:/qt/qml/nga_demo/Colors.qml" // same error as above
Thanks for any clarification.
-
@mzimmers said in issues with using qt_policy(SET QTP0001 NEW):
I don't know what you mean by "import the URI"
qt_add_qml_module(appnga_demo URI nga_demo # <-- This is your URI
So, you do
import nga_demo
import "nga_demo/Colors.qml" // runtime error: no such directory import "qrc:/nga_demo/Colors.qml" // same error as above import "qrc:/qt/qml/nga_demo/Colors.qml" // same error as above
We import QML modules; we don't import individual QML files (I can see why this is confusing, since you imported a *.js file)
-
@JKSH oh, I see...I think I'm starting to see the value in this approach.
So, I replaced all my import references from Color to nga_demo, which got rid of those runtime errors. Now, how do I go about referencing the properties defined in Colors.qml? Lines like this:
property color textColor: checked ? Colors.primaryPurple : Colors.primaryText
Don't work any more. Is Colors still a module, or is it now part of a larger module nga_demo?
Thanks...
-
@mzimmers said in issues with using qt_policy(SET QTP0001 NEW):
@JKSH oh, I see...I think I'm starting to see the value in this approach.
So, I replaced all my import references from Color to nga_demo, which got rid of those runtime errors.
Great!
Now, how do I go about referencing the properties defined in Colors.qml? Lines like this:
property color textColor: checked ? Colors.primaryPurple : Colors.primaryText
Don't work any more. Is Colors still a module, or is it now part of a larger module nga_demo?
It should work.
Colors
is not a module, it is a QML class/singleton inside thenga_demo
module.What is the error message?
Is
set_source_files_properties(Color.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
written before qt_add_qml_module()? -
@JKSH currently the set_source_files_properties() call is just after the qt_add_qml_module(), but in a small test project I tried it both ways with the same (bad) results.
Just to sanity check, I am no longer using my qml.qrc file, as my CMakeLists.txt file contains:
set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON)
This is OK, right?
Thanks...
-
@JKSH said in issues with using qt_policy(SET QTP0001 NEW):
What is the error message?
Please share the message
@mzimmers said in issues with using qt_policy(SET QTP0001 NEW):
in a small test project
Please share your small test project
-
@JKSH error message is: Unable to assign [undefined] to QColor.
CMakeLists.txt (mostly boilerplate from Creator):
cmake_minimum_required(VERSION 3.16) project(newqml VERSION 0.1 LANGUAGES CXX) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Qt6 6.5 REQUIRED COMPONENTS Quick) qt_standard_project_setup(REQUIRES 6.5) qt_add_executable(appnewqml main.cpp ) qt_add_qml_module(appnewqml URI newqml VERSION 1.0 QML_FILES Main.qml Colors.qml ) set_source_files_properties(Color.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE) set_target_properties(appnewqml PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE ) target_link_libraries(appnewqml PRIVATE Qt6::Quick ) install(TARGETS appnewqml BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
main.cpp:
#include <QGuiApplication> #include <QQmlApplicationEngine> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed, &app, []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.loadFromModule("newqml", "Main"); return app.exec(); }
Main.qml:
import QtQuick import QtQuick.Window import newqml Window { width: 640 height: 480 visible: true title: qsTr("Hello World") color: Colors.cards }
Thanks for looking at this.
-
@mzimmers said in issues with using qt_policy(SET QTP0001 NEW):
qt_add_qml_module(appnewqml URI newqml VERSION 1.0 QML_FILES Main.qml Colors.qml ) set_source_files_properties(Color.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
- It should be "Colors.qml", not "Color.qml"
- Put
set_source_files_properties(...)
before qt_add_qml_module()
-
@JKSH oops.
OK, definitely making progress here. It works in my minimal program, and I'm no longer getting the "undefined" error in my real program.
Several of my qml files are in a subdirectory "custom," which has its own CMakeFiles.txt file:
cmake_minimum_required(VERSION 3.16) project(custom) qt_add_qml_module(custom URI custom VERSION 1.0 QML_FILES Roundbutton_custom.qml Scrollbar_custom.qml Switch_custom.qml Tabbutton.qml STATIC )
My main CMakeLists.txt file has an entry:
add_subdirectory(custom)
and this all worked fine before I started switching over to 6.5. Do I need to do something extra for the files in this subdirectory? There's nothing special about them (no singletons, etc.)...they're just regular qml files.
Thanks...
EDIT:
I realize this no longer pertains to the original question in this topic; would it be better if I opened a new topic for this?
-
@mzimmers said in issues with using qt_policy(SET QTP0001 NEW):
OK, definitely making progress here. It works in my minimal program, and I'm no longer getting the "undefined" error in my real program.
Congrats!
Several of my qml files are in a subdirectory "custom," which has its own CMakeFiles.txt file
Notice that this is a different QML module with a different URI.
- main.qml needs to do
import custom
to use Tabbutton - Tabbutton.qml needs to do
import nga_demo
to use Colors
realize this no longer pertains to the original question in this topic; would it be better if I opened a new topic for this?
Yes, best open new threads for new questions :)
- main.qml needs to do
-