8

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.

Community
  • 1
  • 1
vsz
  • 4,811
  • 7
  • 41
  • 78

2 Answers2

7

Add relative path of DefaultText.qml in PanelRight.qml file as

import "../styles"
jotik
  • 17,044
  • 13
  • 58
  • 123
Sumit Jadhav
  • 101
  • 1
0
import QtQuick.Controls.Material
  • 1
    This answer was reviewed in the [Low Quality Queue](https://stackoverflow.com/help/review-low-quality). Here are some guidelines for [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Code only answers are **not considered good answers**, and are likely to be downvoted and/or deleted because they are **less useful** to a community of learners. It's only obvious to you. Explain what it does, and how it's different / **better** than existing answers. [From Review](https://stackoverflow.com/review/low-quality-posts/32393032) – IndieGameDev Aug 05 '22 at 17:48