"Unknown component. (M300)" for custom QML class
-
Summary: Referencing my custom QML widget that is in a different directory results in Qt Creator highlighting it as an error, flagged with the message
"Unknown component. (M300)". However, the QML resolves correctly at runtime without error. How can I fix Qt Creator to not act like a dunce?I have this physical directory structure:
MyProject/ main.qml CommonWidget.qml screens/ Screen1.qmlMy
.qrclooks like this:<RCC> <qresource prefix="/"> <file>main.qml</file> <file>CommonWidget.qml</file> <file alias="Screen1.qml">screens/Screen1.qml</file> </qresource> </RCC>Inside of
Screen1.qmlthe code is roughly:import QtQuick 2.6 Item { CommonWidget { /* ...content here... */ } // Error: Unknown component. (M300) // other stuff }Per this StackOverflow answer I tried adding
import "../screens"to my QML, but that did not change Qt Creator's behavior. -
Summary: Referencing my custom QML widget that is in a different directory results in Qt Creator highlighting it as an error, flagged with the message
"Unknown component. (M300)". However, the QML resolves correctly at runtime without error. How can I fix Qt Creator to not act like a dunce?I have this physical directory structure:
MyProject/ main.qml CommonWidget.qml screens/ Screen1.qmlMy
.qrclooks like this:<RCC> <qresource prefix="/"> <file>main.qml</file> <file>CommonWidget.qml</file> <file alias="Screen1.qml">screens/Screen1.qml</file> </qresource> </RCC>Inside of
Screen1.qmlthe code is roughly:import QtQuick 2.6 Item { CommonWidget { /* ...content here... */ } // Error: Unknown component. (M300) // other stuff }Per this StackOverflow answer I tried adding
import "../screens"to my QML, but that did not change Qt Creator's behavior.@Phrogz
Why addimport "../screens"inScreen1.qml, sinceCommonWidget.qmlis defined in the parent directory of thescreensdirectory?To use
CommonWidgetinScreen1.qml, you can use :import ".."Otherwise, if all is correct, you may need to re-run
qmakefirst to get QtCreator to recognize the new component after adding the file to the project... -
The problem goes away and shows up again as I add new QML. The
importstatement is unnecessary. The (surely overkill) steps I've used to fix it:- Quit Qt Creator
- Delete all output directories from disk
- Delete the
.pro.userfile. - Launch Qt Creator and re-open the project (re-setting up the
.pro.userin the process).
When I do those steps, this problem always goes away.
-
@shaan7 This 2 mouse click solution worked for me, solving the head scratching problem of Qt Creator "unknown component (M300)" errors for ListModel and ListElement. Thanks.