我正在嘗試添加一組自定義屬性以包含在我的 QML 上的多個標簽中。這個想法是創造一種風格,但我不斷收到這些錯誤:
qrc:/TestPage.qml:18:9: Unable to assign [undefined] to QColor
qrc:/TestPage.qml:17:9: Unable to assign [undefined] to double
qrc:/TestPage.qml:16:9: Unable to assign [undefined] to QString
我正在關注下面的執行緒,但對我沒有用。
無法將 [未定義] 分配給 QColor
這是我的標簽:
import "Theme"
Label
{
font.family: Theme.tfont
font.pointSize: Theme.tpointSize
color: Theme.tcolor
text: "text"
}
這是我的 Theme.qml 檔案:
pragma Singleton
import QtQuick
QtObject
{
readonly property string tfont: {return "Calibri"}
readonly property real tpointSize: {return 28}
readonly property string tcolor: {return "black"}
}
任何想法為什么它不會解決財產?
uj5u.com熱心網友回復:
檢查它使用 a來定義全域主題的示例。Style.qml正如 Stephen Quan 已經說過的,你應該創建一個qmldir來定義單例。
這里的重要部分是在你的or中也包含qmldirand the 。Theme.qmlCMakeLists.txtpro
CMakeLists.txt
...
qt_add_qml_module(appuntitled
URI untitled
VERSION 1.0
QML_FILES
main.qml
qmldir
Theme.qml
)
...
qmldir
singleton Theme 1.0 Theme.qml
Theme.qml
pragma Singleton
import QtQuick
QtObject {
readonly property string tfont: "Calibri"
readonly property real tpointSize: 28
readonly property string tcolor: "black"
}
main.qml
import QtQuick
import QtQuick.Controls
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
Label {
font.family: Theme.tfont
font.pointSize: Theme.tpointSize
color: Theme.tcolor
text: "text"
}
}
uj5u.com熱心網友回復:
我對您的應用進行了以下更改:
- 宣布
qmldir - 不需要
import "Theme" - 重構
AppLabel return以宣告方式宣告屬性時不要使用
import QtQuick
import QtQuick.Controls
Page {
AppLabel { text: "text" }
}
//AppLabel.qml
import QtQuick
import QtQuick.Controls
Label {
font.family: Theme.tfont
font.pointSize: Theme.tpointSize
color: Theme.tcolor
}
//qmldir
singleton Theme 1.0 Theme.qml
//Theme.qml
pragma Singleton
import QtQuick
QtObject
{
readonly property string tfont: "Calibri"
readonly property real tpointSize: 28
readonly property string tcolor: "black"
}
您可以在線試用!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/518488.html
標籤:qtqml
