mavlink / qgc-dev-guide

QGroundControl Developers Guide
https://dev.qgroundcontrol.com/en/
Other
42 stars 51 forks source link

Need Help: QGC dev TypeError: Cannot call method 'addMapItem' of undefined #165

Closed omnbmhong closed 1 year ago

omnbmhong commented 1 year ago

this code (code details as below) can serve as draw a circle on map, when we planning the path. I use it in “SimpleItemMapVisual.qml”. it works perfectly. But when I copy and use it in the file “PlanView.qml” , Compiling is OK. But when run this function, it fail and report error: “ TypeError: Cannot call method 'addMapItem' of undefined,"

My Code details: property var map ///< Map control to place item in property var _circle property bool _circleShowing: false

function hideCircle() {
    if (_circleShowing) {
        _circle.destroy()
        _circleShowing = false
    }
}

function showCircle() {
    if (!_circleShowing) {
        _circle = circleComponent.createObject(map)
        map.addMapItem(_circle)
        _circleShowing = true
    }
}

Component { id: circleComponent

                   MapCircle {
                       color:          Qt.rgba(0,0,0,0)
                       border.color:   "yellow"
                       border.width:   6
                       center:         QtPositioning.coordinate()
                       radius:         200
                       visible:        true
                   }
               }

Component.onCompleted: { showCircle() }

Davidsastresas commented 1 year ago

Hello,

QML is tricky to debug, because it always compiles, it is evaluated at runtime, so any issue QML related will only be known when running.

Having said the above, the QML engine is complaining about the map object. You used the map object, which probably was called map on your SimpleItemMapVisual.qml, but most likely on PlanView.qml the reference to map is loaded in a different property. Check how the map object is called in PlanView.qml and use it instead of "map".

If you can not make it work, a link to your branch or something to see more of it could help.

Cheers!

omnbmhong commented 1 year ago

Dear David, Halo Would you kindly specify a little bit more about , how to or any good way, to check the loaded Name of "map object" in PlanView.qml. Many thanks again.

Sent with Proton Mail secure email.

------- Original Message ------- On Monday, December 12th, 2022 at PM 7:01, David Sastre @.***> wrote:

Hello,

QML is tricky to debug, because it always compiles, it is evaluated at runtime, so any issue QML related will only be known when running.

Having said the above, the QML engine is complaining about the map object. You used the map object, which probably was called map on your SimpleItemMapVisual.qml, but most likely on PlanView.qml the reference to map is loaded in a different property. Check how the map object is called in PlanView.qml and use it instead of "map".

If you can not make it work, a link to your branch or something to see more of it could help.

Cheers!

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

Davidsastresas commented 1 year ago

Do you have a branch I can take a look at? I would be easier to point you better in the right direction if I knew more about how and where exactly you placed the code above.

omnbmhong commented 1 year ago

image image image sorry,i dont have a remote brach, i copy the code window, as pictures as above, my own code only lie between line73--108. all the rest code remains as original unchanged. please kindly advise , Many thanks.

omnbmhong commented 1 year ago

your judgement is right, I guess. I try to console.log the value of "map" , it is undefined. I just don't know how to fix it. meanwhile , i also console log the value of "map" in the file "simpleitemmapvisual.qml" , it shows a value of "FlightMap_QMLType_147......" , i dont know how this is imexplicitly assigned this type. I also attached a pic for your reference image thanks a lot.,

omnbmhong commented 1 year ago

Dear David, I try to upload it to my branch this morning ,foryour convinicence. link as: https://github.com/omnbmhong/qgc2022bimcar pls check, PlanView.qml , SimpleItemMapVisual.qml these 2 files. Thanks. Looking forward to your advice.

Davidsastresas commented 1 year ago

I checked it real quick, so I am not really sure if I will be right. You are working over a old version of QGC so I am not really familiarized with how the codebase of your version is.

I do see in planview.qml the map id is editorMap. Try with editorMap.addMapItem, or _root.editorMap.addMapItem.

omnbmhong commented 1 year ago

It Works! Many thanks David. All problem solved. Great advice!

Davidsastresas commented 1 year ago

Great to hear it works, thanks for confirming! I will close the issue now as it is completed. In any case, for the next time, it is better if you post these kind of issues related to custom developments in discord development chat, I think it is more appropriate than github issues for this matter. Best regards!

hamishwillee commented 1 year ago

I think it is more appropriate than github issues for this matter.

Yes. Unless there is something that should be added to the docs coming out of this discussion :-). Thanks for sorting this out @Davidsastresas