JavaEar 专注于收集分享传播有价值的技术资料

QML error "Unknown component. (M300)" but the code works

I want to use a custom font in a QML application, and to not have to specify it in every text field, I use a component as suggested in this answer.

I have a DefaultText.qml under a styles prefix in my qml.qrc, which resides in the folder styles.

import QtQuick 2.0

Text {
    color: "black"
    font.family: myCustomFont.name
    font.bold: false
    font.italic: false
    font.pixelSize: 14
}

I use it, among other places, in a qml named PanelRight.qml, under the prefix Panels in the folder widgets. It's all under the same qml.qrc.

import "qrc:/styles/styles"

Item
{
    // ...
    DefaultText { text: "xyz" }
}

Interestingly, DefaultText is underlined as an error, with the message "Unknown component. (M300)". However, I can successfully compile and run my application, and the custom font is displayed correctly. However, it's annoying that I have a long list of errors (I intend to use it in a lot of places) and that autocomplete doesn't work.

I searched the Qt forums, this problem was mentioned there in case of custom plugins, which I don't use.

1个回答

    最佳答案
  1. Add relative path of DefaultText.qml in PanelRight.qml file as

    import "../styles"