Qt Quick ui 形式不支持函数

functions are not supported in Qt Quick ui form

我试图在我的 Qt Quick 应用程序中打开 link,我收到 functions are not supported in Qt Quick ui form 警告,该应用程序有效,我想摆脱警告,如何修复此警告.

AboutForm.ui.qml 文件

Text {
    id: license
    x: 40
    y: 207
    color: "#ffffff"
    text: qsTr("<a href='https://www.gnu.org/licenses/old-licenses/gpl-2.0.html'>GNU General Public License, version 2 or later</a>")
    font.pixelSize: 16
    // the editor complains about this function
    onLinkActivated: Qt.openUrlExternally("https://www.gnu.org/licenses/old-licenses/gpl-2.0.html")

    MouseArea {
        anchors.fill: parent
        acceptedButtons: Qt.NoButton
        cursorShape: parent.hoveredLink ? Qt.PointingHandCursor : Qt.ArrowCursor
    }
}

我的 About.qml 文件是空的。

import QtQuick 2.4

AboutForm {

}

根据 docs:

You can use Qt Creator wizards to create UI forms that have the filename extension .ui.qml. The UI forms contain a purely declarative subset of the QML language. It is recommended that you edit the forms in the Design mode. However, exporting items as alias properties is a commercial only feature, and therefore you must use the Edit mode to do it if you are using the open source version of Qt Creator. Qt Creator enforces the use of the supported QML features by displaying error messages.

The following features are not supported:

  • JavaScript blocks
  • Function definitions
  • Function calls (except qsTr)
  • Other bindings than pure expressions
  • Signal handlers
  • States in other items than the root item
  • Root items that are not derived from QQuickItem or Item

The following types are not supported:

  • Behavior
  • Binding
  • Canvas
  • Component
  • Shader Effect
  • Timer
  • Transform
  • Transition

我建议的解决方案是通过以下方式抑制警告:

// @disable-check M222
onLinkActivated: Qt.openUrlExternally(
                     "https://www.gnu.org/licenses/old-licenses/gpl-2.0.html")

参考文献:

AboutForm.ui.qml

// add this:    
property alias license: license    

Text {
        id: license
        x: 40
        y: 207
        color: "#ffffff"
        text: qsTr("<a href='https://www.gnu.org/licenses/old-licenses/gpl-2.0.html'>GNU General Public License, version 2 or later</a>")
        font.pixelSize: 16
        // Remove this line


    MouseArea {
        anchors.fill: parent
        acceptedButtons: Qt.NoButton
        cursorShape: parent.hoveredLink ? Qt.PointingHandCursor : Qt.ArrowCursor
    }
}

About.qml

import QtQuick 2.4

AboutForm {
     // add this line
     license.onLinkActivated: Qt.openUrlExternally("https://www.gnu.org/licenses/old-licenses/gpl-2.0.html")
}

Qt Quick UI Form 应该是一个只有可见元素的 QML 文件。这里没有业务逻辑 (Qt-forum)。

当您使用 Qt Creator 创建 Qt Quick UI 表单时,它将创建两个文件:

  1. YourItemForm.ui.qml
  2. YourItem.qml <--- 这包括逻辑。

YourItem.qml:

YourItemForm {
  button1 {
    text: data_provider.get("button_text")
  }
}

YourItemForm.ui.qml:

Item {
  property alias button1 : button1 
  Button {
    id: button1
  }
}

我建议从 main.qml 而不是 UI 本身打开外部 link。

在AboutForm.ui.qml中添加

属性 整数 externalcall:1

        MouseArea {
            id: mousearea
            anchors.fill: parent
            drag.target: parent
            onClicked: externalcall=2
        }

然后在 main.qml 应用程序中 window 创建一个 on externalcall changed 并在此函数中打开 link。如果不清楚,让我举个例子。