解决 QML 导入中的资源歧义 [英] Resolve resource ambiguity in QML imports
问题描述
我需要同时使用 QtLabs 和 QtQuickControls.两者都有 Button
类型,但我需要在 QuickControls 中使用那个.QML 文件正在挑选实验室中的按钮.如何强制它使用 QuickControls 中的那个?
I need to use both QtLabs and QtQuickControls. Both have the Button
type but I need to use the one in QuickControls. The QML file is picking the button in labs. How do I force it to use the one in QuickControls?
import QtQuick 2.6
import QtQuick.Controls 1.5 //This is what I need the QML file to pick button from
import QtQuick.Controls.Styles 1.4
import QtGraphicalEffects 1.0
import QtQuick.Dialogs 1.2
import QtMultimedia 5.6
import Qt.labs.controls 1.0 //This is where it is picking Button from
推荐答案
解决问题的一种快速/简单的方法是使用 as
关键字进行命名导入.为导入命名后,模块中的所有组件都可以通过该名称访问.
A fast/easy way to solve the issue is to make a named import with the as
keyword. After you give a name to the import all the components in the module can be accessed through that name.
导入示例:
import QtQuick 2.6
import QtQuick.Controls 1.5 as Ctrl1 //name for old controls
import QtQuick.Controls.Styles 1.4
import QtGraphicalEffects 1.0
import QtQuick.Dialogs 1.2
import QtMultimedia 5.6
import Qt.labs.controls 1.0 as Ctrl2 //name for new controls
Ctrl2.ApplicationWindow {
id: root
visible: true
width: 400
height: 300
Column {
anchors.fill: parent
Ctrl1.Button {
text: qsTr("one")
}
Ctrl2.Button {
text: qsTr("two")
}
}
}
这种方法很容易变得过于冗长.在这种情况下,我会将不同文件中的内容分开,物理分离有问题的导入.
This approach can easily become too verbose. In that case I would separate the content in different files, physically separating the offending imports.
这篇关于解决 QML 导入中的资源歧义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!