QML,由于使用另一个qml文件的组件而失败

时间:2018-12-02 10:57:15

标签: qt menu qml

我在使用另一个qml文件中的qml组件时遇到问题。我有2个文件,main.qmlmenu.qmlmenu.qml在另一个名为menu的文件夹中)。在menu.qml中,有我要在main.qml

中使用的菜单
import QtQuick 2.9
import QtQuick.Controls 2.4

Menu {
    id: routingMenu
    width: maximumWidth
    height: 200
    y: 20
    cascade: true
    Rectangle {
        Label {
            x: (app_window.width / 8)
            text: "FROM:"
            font.pixelSize: 22
            font.italic: true
            color: "black"
        }
        Label {
            y: routingMenu.height / 7
            text: "Country:"
            font.pixelSize: 22
            font.italic: true
            color: "black"
        }
        Label {
            y: routingMenu.height / 2.5
            text: "City:"
            font.pixelSize: 22
            font.italic: true
            color: "black"
        }
        Label {
            y: routingMenu.height / 1.6
            text: "Street:"
            font.pixelSize: 22
            font.italic: true
            color: "black"
        }
        Label {
            y: routingMenu.height / 1.2
            text: "Postal Code:"
            font.pixelSize: 22
            font.italic: true
            color: "black"
        }
    } //Rectangle

    Rectangle {
         x: app_window.width / 2
         Label {
             x: (app_window.width / 4)
             text: "TO:"
             font.pixelSize: 22
             font.italic: true
             color: "black"
         }
         Label {
             y: routingMenu.height / 7
             text: "Country:"
             font.pixelSize: 22
             font.italic: true
             color: "black"
         }
         Label {
             y: routingMenu.height / 2.5
             text: "City:"
             font.pixelSize: 22
             font.italic: true
             color: "black"
        }
        Label {
            y: routingMenu.height / 1.6
            text: "Street:"
            font.pixelSize: 22
            font.italic: true
            color: "black"
        }
        Label {
            y: routingMenu.height / 1.2
            text: "Postal Code:"
            font.pixelSize: 22
            font.italic: true
            color: "black"
        }
    } //Rectangle

    Button {
        id: sendDataToItem
        width: 100
        height: 40
        x: app_window.width / 2.7
        text: "Send"

        onClicked: {
            //fromAddress.country: "";
            //fromAddress.city: "";
            //fromAddress.street: "";
            //fromAddress.postalCode: "";

            //toAddress.country: "";
            //toAddress.city: "";
            //toAddress.street: "";
            //toAddress.postalCode: "";
        }
    } //sendDataToItem
} //Menu

我尝试使用组件,但是它没有帮助我。那是我的main.qml的代码

import QtQuick 2.10
import QtQuick.Controls 2.2
import QtQuick.Window 2.0
import QtLocation 5.6
import QtPositioning 5.6
import "menu"

ApplicationWindow {
    id: app_window
    visible: true
    width: maximumWidth
    height: maximumHeight
    title: qsTr("Navigation")


    PositionSource {
           active: true
           onPositionChanged: {
           //map_id.center = position.coordinate;
           }
    }

    Plugin {
        id: mapPlugin_
        name: "osm"
    }

    /*Loader {
            id: loadered
            focus: true
            source: "menu/menu.qml"
            active: true
        }*/


    Rectangle {
         id: mapRectangleID
         width: 800
         height: 800
         x:0
         y:20

         Map {
            id: map_
            anchors.fill: parent
            plugin: mapPlugin_
            center: QtPositioning.coordinate(51.320729390711975,12.280097007751465)
            zoomLevel: 15

            MapQuickItem {
                       id: marker_id
                       coordinate: QtPositioning.coordinate(59.91, 10.75)
                       sourceItem: Image {
                               id: endPointImage
                               source: "assets/marker.png"
                               width: 100
                               height: 100
                       } //size and position of maker
                       anchorPoint.x: endPointImage.width / 2
                       anchorPoint.y: endPointImage.height
            } //marker

            RouteModel {
                    id: routeBetweenPoints
                    plugin: Plugin { name: "osm" }
                    query: RouteQuery {id: routeQuery }
                    Component.onCompleted: {
                    routeQuery.addWaypoint(QtPositioning.coordinate(51.318784,12.2773504 ));
                    routeQuery.addWaypoint(QtPositioning.coordinate(51.3117764,12.280909000000065 ));
                       //routeQuery.addWaypoint(endPointGeaocodeModel)
                    update();
                    }
            } //start and end point

            MapItemView {
                model: routeBetweenPoints
                delegate: Component {
                    MapRoute {
                        route: routeData
                        line.color: "red"
                        line.width: 10
                    }
                } //Component
            }//linie, die beide punkte verbindet

            GeocodeModel{
                 id: endPointGeaocodeModel
                 plugin: Plugin { name: "osm" }
                 query: "Sandakerveien 116, Oslo"
                 onLocationsChanged: {
                     if (count> 0){
                        marker_id.coordinate = get(0).coordinate
                        map_id.center = get(0).coordinate
                     }
                 }
                     Component.onCompleted: update()
            } //suche den platz mit strasse und stadt








            //! [geocode0]
            Address {
                id: fromAddress
                city: ""
                country: ""
                street: ""
                postalCode: ""
            } //fromAddress
            //! [geocode0]

            Address {
                id: toAddress
                country: ""
                city: ""
                street: ""
                postalCode: ""
            } //toAddress



         } //Map
    } //mapRectangleID

    Button {
        id: btn_close
        width: 100
        height: 20
        text: "Close"

        onClicked: {
           Qt.quit();
        }
    }

    Button {
        id: btn_routing
        width: 100
        height: 20
        x:100
        text: "Routing"

        onClicked: {
             routingMenu.open();
        }
    }

    Button {
        id: btn_oldWay
        width: 100
        height: 20
        x:200
        text: "Old way"

        onClicked: {
             oldWayMenu.open();
        }
    }

    Button {
        id: rest
        width: parent.width - x
        height: 20
        x:300
        text: ""

        onClicked: {
        }
    }
} //ApplicationWindow

我尝试了2种方法来解决问题。一个使用Loader,但无法打开菜单,另一个使用QT的示例,该示例位于MapViewer示例中。他们仅导入订单并使用组件的ID。但这是行不通的。我认为,我在代码中做错了什么,有人可以告诉我我做错了什么,还是向我展示解决问题的正确方法。

我认为代码中最重要的部分是:

Loader {
        id: loadered
        focus: true
        source: "menu/menu.qml"
        active: true
    }

然后,我如何使用它:

Button {
    id: btn_routing
    width: 100
    height: 20
    x:100
    text: "Routing"

    onClicked: {
         routingMenu.open();
    }
}

通过按钮,我正在使用组件名称,因为我导入了菜单。但是,如果我想使用加载程序,则将加载程序中的visible设置为false,然后将onClicked设置为true。但是我尝试了一下,但是没有用。

感谢帮助 DC

1 个答案:

答案 0 :(得分:0)

该ID仅在.qml之外是有效的,它没有任何意义,例如,假设您要创建4个菜单,那么如果您要使用ID路由菜单,那么您将引用哪个菜单?因为那会引起混乱。因此,如果要使用Loader加载的项目,则必须使用item属性。

Loader {
    id: loadered
    focus: true
    source: "menu/menu.qml"
    active: true
}
// ...

Button {
    id: btn_routing
    width: 100
    height: 20
    x:100
    text: "Routing"
    onClicked: loadered.item.open()
}