Solved "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.qml
My
.qrc
looks 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.qml
the 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.qml
is defined in the parent directory of thescreens
directory?To use
CommonWidget
inScreen1.qml
, you can use :import ".."
Otherwise, if all is correct, you may need to re-run
qmake
first 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
import
statement is unnecessary. The (surely overkill) steps I've used to fix it:- Quit Qt Creator
- Delete all output directories from disk
- Delete the
.pro.user
file. - Launch Qt Creator and re-open the project (re-setting up the
.pro.user
in the process).
When I do those steps, this problem always goes away.
-
Did you try
Tools > QML/JS > Reset Code Model
? Usually works for me. -
@shaan7
thanks this was really helpful -
@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.
-
@shaan7 Worked for me too. I was also having the problem of "unknown component (M300)" error for ListModel and ListElement. Thanks.
-
@shaan7 thanks!perfect solution!