0

I have QML component with TextEdit:

TextEdit {
    ...
    font: settings.myFont
    font.preferShaping: false

when load the component I get error qml Can't create component <path_to_my_qml>: Property has already been assigned a value pointing to the line font.preferShaping: false.

I try to comment the line font.preferShaping: false and set the property on the c++ side by:

QFont Settings::myFont() const
{
    ...
    font.setStyleStrategy(QFont::PreferNoShaping);    
    return font;
}

but then the appearance of the text matches the default value of the font.preferShaping property, which is true.

How can I set the font.preferShaping property to false without errors?


Ultimately, I need to achieve the same display of a text in QML and on c++ side, where the text draws via updatePaintNode:

QSGNode* Text::updatePaintNode(QSGNode* oldNode, QQuickItem::UpdatePaintNodeData*)
{
    ...
    painter.setFont(settings.myFont());
    painter.drawText(rect, alignment(), m_textParams.text());
    ...
}
Vladimir Bershov
  • 2,701
  • 2
  • 21
  • 51
  • Unfortunately, the problematic line is the setting of the `font` property which yields undesirable behavior so, your symptoms are actually unsurprising. The solution is you need to deep copy your font properties, e.g. `font.family: settings.myFont.family` and so forth. – Stephen Quan Apr 27 '23 at 07:16
  • 1
    Somewhat similar question here: https://stackoverflow.com/questions/71362575/cant-use-qfont-type-property-values-when-extending-qml-properties – Stephen Quan Apr 27 '23 at 07:47
  • the line `font: settings.myFont` is problematic ? – Vladimir Bershov Apr 27 '23 at 08:04
  • Yes. My understanding is the `font` property feels like an object, and you assign it feels like it is destroying the underlying object and creating a new one. After that happened, I've noticed unpredictable things happen. So, the recommendation is try not to do that, hence the recommendation to deep copy the properties within. – Stephen Quan Apr 27 '23 at 08:38

1 Answers1

0

How about setting the property after the onFontChanged signal is emitted:

Text {
   id: text
   font: settings.myFont
   onFontChanged: {
       text.font.preferShaping = false;
   }
}
Jürgen Lutz
  • 329
  • 1
  • 1
  • 10
  • no it doesn't help – Vladimir Bershov Apr 27 '23 at 10:25
  • Interesting. If I do it like this I can check preferedShaping and it is set to false...the font has to be diffenrent to the old one. Otherwise the function onFontChanged is not called. Have you checked, if the signal is emitted? – Jürgen Lutz Apr 27 '23 at 11:35
  • You could use Component.onCompleted to set the flag. This handler is called in any case. – Jürgen Lutz Apr 27 '23 at 11:40
  • it is not possible to use both `font` and `font.preferShaping` properties: https://stackoverflow.com/questions/71362575/cant-use-qfont-type-property-values-when-extending-qml-properties – Vladimir Bershov Apr 27 '23 at 12:48