关于 QML Invalid property name(M16)解决方案

问题描述

最近在学习QML,发现在使用某些属性的时候会出现:

     invalid property name component m16    

的情况,如下:

在网上也找了一些方法,发现是在QtCreator 中编辑qml文件时,由于某些模块导入的信息不完整才导致了这些红色提示,但是能正常运行。

以下将两种解决办法(推荐使用第二种)。

解决办法

Solution1:

在爆红代码前加上:   // @disable-check M16  即可,如下:

  • 注意:如果按照这种方式,可能同样也会禁用掉真正没有的属性,导致错误

Solution2:

造成这种问题是因为QML中某些属性没有正确被正常加载,解决这个问题只需在顶部导入对应的模块,为了省事,可以直接导入对应的QML版本即可:如下我的版本是2.12,导入QtQml 2.12即可:

import QtQml 2.12

报红线部分消失,运行结果如下:
 

至此问题就解决了。

在您提供的代码中,出现了一个错误。根据您提供的错误信息,看起来是因为在 ColumnLayout 中使用了 anchors 属性,而该属性实际上是无效的。 ColumnLayout 是 Qt Quick Controls 2 中的一种布局,它不支持 anchors 属性。如果您想在 ColumnLayout 中设置控件的位置和大小,可以使用 Layout 的属性和方法来实现。 例如,您可以使用 Layout.alignment 来设置控件的水平对齐方式,使用 Layout.fillWidth 和 Layout.fillHeight 来设置控件是否填充父控件的宽度和高度。 以下是一个修改后的示例代码: ```qml ColumnLayout { Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter // 设置布局的对齐方式 Image { id: channelImage source: "qrc:/images/ze-apps-o Copy 3@1x_1.png" width: parent.height / 2 height: parent.height / 2 fillMode: Image.PreserveAspectFit Layout.alignment: Qt.AlignHCenter // 设置控件的水平对齐方式 } Text { id: channelText text: "Channel" horizontalAlignment: Text.AlignHCenter verticalAlignment: Text.AlignVCenter color: "white"; font.pixelSize: parent.height * 6 * rZoomRatio; font.weight: Font.DemiBold font.bold: true } MouseArea { anchors.fill: parent // 添加 MouseArea 来接收点击事件 onClicked: { appStackView.pop() appStackView.push(channelComponent) channelText.color = "pink" homeText.color = "white" mixerText.color = "white" channelImage.source = "qrc:/images/ze-apps-o Copy 3@1x.png" homeImage.source = "qrc:/images/antOutline-home@1x.png" mixerImage.source = "qrc:/images/Copy@1x.png" } } } ``` 请注意,我添加了一个 MouseArea 来接收点击事件,并将其设置为填充整个父控件。这样,当用户点击 ColumnLayout 区域时,就会触发 onClicked 事件。 希望这可以帮助到您!如果您有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值