qt - 将自定义控件移动到自己的文件中时出现 QML TypeError
问题描述
我已经按照文档成功地设计了一个 QML Switch,它运行良好。我现在想重用我的样式开关,但是当我将开关代码移动到它自己的文件中时,我得到了这些错误:
qml/QtQuick/Controls/Switch.qml:124: TypeError: Cannot read property 'max' of null
qml/QtQuick/Controls/Switch.qml:123: TypeError: Cannot read property 'min' of null
qml/QtQuick/Controls/Switch.qml:122: TypeError: Cannot read property '__handle' of null
MySwitch.qml:28: TypeError: Cannot read property 'switchStyle' of undefined
MySwitch 代码如下:
import Felgo 3.0
import QtQuick 2.12
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.12 as Q
Q.Switch {
id: switchControl
indicator: Rectangle {
implicitWidth: 48 +textOff.width - 5
implicitHeight: 26
x: parent.leftPadding //sc
y: parent.height / 2 - height / 2
radius: 13
color: parent.checked ? "black" : "white" //sc
border.color: "black"
// Control center circle
Rectangle {
x: parent.parent.checked ? parent.width - width : 0
width: 26
height: 26
radius: 13
color: "white"
border.color: "black"
} // <-- This is line 28
// On text
MultiResolutionImage {
anchors.verticalCenter: parent.verticalCenter
x: 13 -5
fillMode: Image.PreserveAspectFit
source: "../../assets/img/scenesettings/texton.png"
visible: parent.checked //sc
}
// Off text
MultiResolutionImage {
id: textOff
anchors.verticalCenter: parent.verticalCenter
x: parent.width - textOff.width - 13 + 5 // Outside of radius
fillMode: Image.PreserveAspectFit
source: "../../assets/img/scenesettings/textoff.png"
visible: !parent.checked // switchControl
}
} // Indicator
} // Switch
有人可以解释什么是错的吗?
解决方案
为防止名称冲突,您可以使用as
关键字为导入添加前缀名称,如下所示:
import Felgo 3.0 as MyLibrary
然后为了使用该模块中的任何内容,您将需要使用该名称:
MyLibrary.MultiResolutionImage {
...
}
推荐阅读
- html - 将使用正则表达式清理的调整后的 ASP 变量插入 meta content="" 字段
- bash - 在 bash 终端上逐页查看 kubectl 日志
- firebase - Redux 状态在新组件加载时被重置/覆盖
- c# - 如何通过软件取消链接没有 GroupBox 的所有单选按钮
- python - Python 集合与数组
- c - 如何在 xinetd 自定义服务中获取已连接客户端的 IP 地址
- java - 如何仅反转句子中单词中的字符(留下数字)?
- r - 在 RStudio 中不推荐使用 plotly 图表的问题
- c++ - 声明不兼容
- haskell - 在 Haskell 中创建没有参数的函数时解析错误