Resolve resource ambiguity in QML imports

后端 未结 1 1539
不知归路
不知归路 2021-01-22 13:38

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

相关标签:
1条回答
  • 2021-01-22 14:09

    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.

    Example with your imports:

    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.

    0 讨论(0)
提交回复
热议问题