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

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.

Add relative path of DefaultText.qml in PanelRight.qml file as
import "../styles"

import QtQuick.Controls.Material

Related

How to change the font in the SpTextPresenter?

Pharo 9, Spec 2 -- I have a Spec 2 presenter with a text widget:
initializePresenters
text := self newText.
super initializePresenters
As I understand its type is SpTextPresenter. How to change the font of this text? Font face, size of the all shown text in this widget... For example, to "Courier New", 9.
EDIT 1:
Also I tried:
text addStyle: { SpStyleSTONReader fromString:
'
Font {
#name: "Source Sans Pro",
#size: 12,
#bold: false,
#italic: true
}' }.
but it does not work, the error is: Improper store into indexable object.
EDIT 2:
Also I found this documentation. It seems that the scenario must be:
Read styles as STON
Set styles somwhere (where?) for the all application. They are described under its names in the STON so they can be referred under its names in the application.
Call addStyle: 'the-name' so the widget with a name the-name will refer own styles from the loaded STON.
The problem is in 2. - I have not application, just one presenter which I open with openWithSpec.
I didn't notice this 'till now.
Spec "styles" cannot be added directly to the component but they need to be part of a stylesheet.
Stylesheets are defined in your application (in particular in your application configuration).
You can take a look at StPharoApplication>>resetConfiguration, StPharoMorphicConfiguration>>styleSheet and StPharoMorphicConfiguration>>styleSheetCommon as examples (you will also see there than using STON to declare your styles is just a convenience way, not mandatory).
Here a simplified version of what you will find there:
StPharoApplication >> resetConfiguration
self useBackend: #Morphic with: StPharoMorphicConfiguration new
StPharoMorphicConfiguration >> styleSheet
^ SpStyle defaultStyleSheet, self styleSheetCommon
StPharoMorphicConfiguration >> styleSheetCommon
"Just an example on how to build styles programatically ;)"
^ SpStyleSTONReader fromString: '
.application [
.searchInputField [
Font { #size: 12 }
]
]
'
Then you can add the style to your component:
text addStyle: 'searchInputField'

Is it possible to show QML controls boundaries?

When developing a QML application I think it can sometime be useful if I was able to set some setting to outline all visual elements boundaries. For instance a control in Qt Quick Controls 2.x might consist of several parts like background, contentItem, indicators etc. When tweaking on the size of these I would like to see the boundaries of each of these parts.
Is there any functionality like this in Qt/QML?
Three years later, and folks (specifically: me) are still doing web searches about this :)
Just like commenter #DuKes0mE suggested, I have "made do" by adding borders to things on-the-fly and then removing them from the final code.
Like the OP, I am now tired of doing that.
A tactic I arrived at recently is to add a DebugRectangle.qml custom element to my project:
import QtQuick 2.12
Rectangle {
property var toFill: parent // instantiation site "can" (optionally) override
property color customColor: 'yellow' // instantiation site "can" (optionally) override
property int customThickness: 1 // instantiation site "can" (optionally) override
anchors.fill: toFill
z: 200
color: 'transparent'
border.color: customColor
border.width: customThickness
}
Then I can add it to existing elements like so, to debug them:
Label {
text: 'Lorem ipsum dolor sit amet'
}
Label {
text: 'quis nostrud exercitation'
DebugRectangle {} // Adds "debug border" to this Label
}
And when I am finished, I can even leave the nested DebugRectangle in the code, but toggle its visibility like so:
Label {
text: 'quis nostrud exercitation'
DebugRectangle {
visible: false
}
}
Complete sample project shared on GitHub.
There's a tool called GammaRay which (amongst other things) allows investigating QtQuick 2 applications, see:
http://doc.qt.io/GammaRay/gammaray-qtquick2-inspector.html
Setup instructions are here:
https://github.com/KDAB/GammaRay
If you're running Linux, it is quite likely your distribution already ships a GammaRay package.

Placeholder text in QML TextEdit

I am looking for a way to show a text hint stating the expected input as advice for the user. Take the Google search bar for example:
Is there a property I am missing, or is this something that has to be achieved through scripting?
The property doesn't exist on the Qt Quick input items. You can vote for the feature here.
In the meantime, you can use TextArea from Qt Quick Controls 2.
If you would rather use pure Qt Quick, you can do something similar to what the Controls do and add a Text item above the field:
import QtQuick 2.5
import QtQuick.Window 2.2
Window {
width: 300
height: 300
visible: true
TextEdit {
id: textEdit
width: 200
height: 50
property string placeholderText: "Enter text here..."
Text {
text: textEdit.placeholderText
color: "#aaa"
visible: !textEdit.text
}
}
}
This is kinda old but I found another necessity for Android builds.
Since Android only send the text editing signal after you press ok in virtual keyboard, the placeholder remains there. So to avoid it, I recommend:
TextEdit {
id: textEdit
width: 200
height: 50
property string placeholderText: "Enter text here..."
Text {
text: textEdit.placeholderText
color: "#aaa"
visible: !textEdit.text && !textEdit.activeFocus // <----------- ;-)
}
}
If you want one line input then why not use TextField ?

Link in Label BlackBerry 10

I am using JSON to receive data and place it into List. There is a Label displaying the text that I am receiving from the JSON. In some of the cases there is a Link in the text. By default you can't click on the Link from the label. Is there a way to make the Link to be clickable?
Label {
text: "Click here to open browser and get redirected to www.stackoverflow.com";
}
The output is "Click here to open browser and get redirected to www.stackoverflow.com" but the Link to StackOverflow is not clickable.
Use TextArea instead of Label and set property editable to false, it would look same as Label.
Don't forget to set inputMode to either Text or Chat.
TextArea {
text: "http://www.google.com"
editable: false
inputMode: TextAreaInputMode.Text
}
You can actually use HTML in the label itself to style the text as a link, according to the Text Styles documentation. You need to be aware of a few quirks though if you are going to apply any of your own styles, as discussed on the Blackberry Developer support forums here. The example below should work, using the default style which will colour the link blue, with bold and underline:
Label {
text: "<html>Click here to open browser and get redirected to <a href='http://www.stackoverflow.com'>www.stackoverflow.com</a></html>"
}
Note: you may need to set multiline: true on the Label in order to see all of the text, depending on your layout.
You should assign Text.RichText value to "textFormat" property of the Label:
import QtQuick 1.1
Rectangle {
width: 360
height: 360
Text {
text: "Click here"
anchors.centerIn: parent
textFormat: Text.RichText
onLinkActivated: {
Qt.openUrlExternally(link)
}
}
}

reStructuredText styles

I found this guide for rst2pdf to find out how to style a reStructuredText file in the resulting pdf document. Having the following in my JSON stylesheet, for example, it is successfully applied to the whole document:
"pageSetup" : {
"size": "A4",
"width": null,
"height": null,
"margin-top": "2cm",
[...]
"margin-gutter": "0cm"
}
How is a particular style applied only to a specific class? For example, how can I apply a particular font the to the h1 class? My immediate difficulty stems from the fact that I'm unsure about whether it's actually called h1, H1, header1, or Header1.
The rst2pdf.py manual does not seem very informative with regards to the style names. However, the section on Styles (chapter 8) has this example:
["heading1" , {
"parent": "normal",
"fontName": "Tuffy_Bold",
"fontSize": 18,
"keepWithNext": true,
"spaceAfter": 6
}],
So it seems that heading1 is the appropriate style name.
One thing to note is that
If your document requires a style that is not defined in your stylesheet, it will print a warning and use bodytext instead.
So presuming that you don't get any warnings when generating your document the styles must be set in the default stylesheet, so have a look through this to get a feel for the style names used.
You can make rst2pdf print the default stylesheet using
rst2pdf --print-stylesheet
If you want to add styles, just create a stylesheet, (or take the standard stylesheet and modify it) and pass it with the -s option
rst2pdf mydoc.txt -s mystyles.txt