QML-无法分配给不存在的属性“样式" [英] QML - Cannot assign to non-existent property "style"

查看:844
本文介绍了QML-无法分配给不存在的属性“样式"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Qt 5.10.1与Qt Creator 4.5.1结合使用,并且 style 属性在元素中永远不可用.

I'm using Qt 5.10.1 with Qt Creator 4.5.1 and the style property is never available in elements.

例如,如下所示 ButtonStyle QML类型,我想这样做:

For example, as shown here ButtonStyle QML Type , I would like to do:

Button {
    text: "A button"
    style: ButtonStyle {...}
}

但是,我得到了错误:

无法分配给不存在的属性样式"

Cannot assign to non-existent property "style"

我尝试使用矩形,进度条,但遇到相同的错误.

I tried with a rectangle, progressbar and I get the same error.

编辑#1:

我确实有所有这些进口.如果导入丢失,我会在 ButtonStyle 上看到错误,但错误是在 style 上.

I do have all these imports. If the import was missing, I would get the error on ButtonStyle , but the error is on style.

import QtQuick 2.2
import QtQuick.Controls 2.3
import QtQuick.Dialogs 1.0
import QtGraphicalEffects 1.0
import QtQuick.Shapes 1.0
import QtQuick.Layouts 1.3
import QtQuick.Controls.Styles 1.4

推荐答案

在QML中,按钮有两种类型:

There are 2 types of Buttons in QML:

  • Button Qt Quick Controls 2: https://doc.qt.io/qt-5.10/qml-qtquick-controls2-button.html
  • Button Qt Quick Controls: http://doc.qt.io/qt-5/qml-qtquick-controls-button.html

在您的情况下,您要导入Qt QuickControls 2按钮:import QtQuick.Controls 2.3,并且该按钮不具有style属性.

In your case, you are importing the Qt QuickControls 2 Button: import QtQuick.Controls 2.3, and that Button does not have the style attribute.

如果需要使用样式,则必须导入:

If you need to use the style you must import:

import QtQuick.Controls 1.4

代替:

import QtQuick.Controls 2.3


如果您使用的是Qt Quick Controls和Qt Quick Controls 2中的项目,则可以使用名称空间将它们分开:


If you are using items from Qt Quick Controls and Qt Quick Controls 2 you could separate them using a namespace:

import QtQuick.Controls 2.3 as QQC2
import QtQuick.Controls 1.4 as QQC1

QQC1.Button {
    text: "A button"
    style: ButtonStyle {...}
}

QQC2.another_item_of_Qt_Quick_Controls2{
}

这篇关于QML-无法分配给不存在的属性“样式"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆