Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add marker event #29

Open
baspinarenes opened this issue Feb 5, 2021 · 13 comments
Open

Add marker event #29

baspinarenes opened this issue Feb 5, 2021 · 13 comments

Comments

@baspinarenes
Copy link

Hi. Your project is very helpful, thank you. I want to define a drag event for markers. I define a signal like you did, but I get an error "js: Uncaught TypeError: Cannot read property '_onDrag' of undefined". What am I doing wrong?

marker.py

drag = pyqtSignal(dict)
...
@pyqtSlot(QJsonValue)
    def _onDrag(self, event):
        self._logger.debug('draged. event: {event}'.format(event=event))
        self.drag.emit(self._qJsonValueToDict(event))
...
    def __init__(self, latLng, options=None):
        ...
        self._connectEventToSignal('drag', '_onDrag')
@JaWeilBaum
Copy link

Have a look at #30 I added 2 events there

@baspinarenes
Copy link
Author

baspinarenes commented Feb 11, 2021

Have a look at #30 I added 2 events there

Thanks for be interested. But I still get an "js: Uncaught TypeError: Cannot read property '_onMove' of undefined" error. I think the error is in this function:

def _connectEventToSignal(self, event, signalEmitter):
        # We need to delete some keys as they are causing circular structures
        js = '{name}.on("{event}", function(e) {{\
                  delete e.target;\
                  delete e.sourceTarget;\
                  e = copyWithoutCircularReferences([e], e);\
                  channelObjects.{name}Object.{signalEmitter}(e)}})'.format(
            name=self.jsName, event=event, signalEmitter=signalEmitter)
        self.runJavaScript(js)

@JaWeilBaum
Copy link

What do you do before getting this error?

@baspinarenes
Copy link
Author

baspinarenes commented Feb 11, 2021

I don't get the error moving Maker before adding the code you provided. However, when I add the code you provide, I get this error when the move and moveend event are triggered.

@JaWeilBaum
Copy link

If you try this samlpe code, does the same error occur?

import sys
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget
from pyqtlet import L, MapWidget


class MapWindow(QWidget):
    def __init__(self):
        # Setting up the widgets and layout
        super().__init__()
        self.mapWidget = MapWidget()
        self.layout = QVBoxLayout()
        self.layout.addWidget(self.mapWidget)
        self.setLayout(self.layout)

        # Working with the maps with pyqtlet
        self.map = L.map(self.mapWidget)
        self.map.setView([12.97, 77.59], 10)
        L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(self.map)
        self.marker = L.marker([12.934056, 77.610029], options={"opacity": 0.5, "draggable": 'true'})
        self.marker.bindPopup('Maps are a treasure.')
        self.marker.move.connect(self.move) #new listener
        self.marker.moveend.connect(self.move_end)  #new listener
        self.map.addLayer(self.marker)
        self.show()

    def move(self, event):

        print('move', event)

    def move_end(self, event):

        print('Move end', event["sender"].latLng)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    widget = MapWindow()
    sys.exit(app.exec_())

@baspinarenes
Copy link
Author

Yes, it worked fine. I guess there is an indirect error in me, I will need to examine it in more detail. Thank you for your attention.

@JaWeilBaum
Copy link

No worries. You can also send me your code, maybe I see something. I'm looking forward to implement more signals, since got some use for this.

@baspinarenes
Copy link
Author

No worries. You can also send me your code, maybe I see something. I'm looking forward to implement more signals, since got some use for this.

My project is available on Github: https://github.com/hurturkiha/hurturk-otopilot-arayuzu. I am trying to create a uav ground control station. The codes can be a little confusing to examine. Let me not bother you, I will look in detail and try to find the part that creates the error.

@baspinarenes
Copy link
Author

baspinarenes commented Feb 12, 2021

I found the problem. If we define the marker as manual, it works without any trouble. However, it gives an error when it defines the click event.

import sys
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget
from pyqtlet import L, MapWidget


class MapWindow(QWidget):
    def __init__(self):
        # Setting up the widgets and layout
        super().__init__()
        self.mapWidget = MapWidget()
        self.layout = QVBoxLayout()
        self.layout.addWidget(self.mapWidget)
        self.setLayout(self.layout)

        # Working with the maps with pyqtlet
        self.map = L.map(self.mapWidget)
        self.map.setView([12.97, 77.59], 10)
        L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(self.map)
        self.map.clicked.connect(self.add_marker)
        self.marker = L.marker([12.934056, 77.610029], options={"opacity": 0.5, "draggable": 'true'})
        self.marker.move.connect(self.move) #new listener
        self.marker.moveend.connect(self.move_end)  #new listener
        self.map.addLayer(self.marker)
        self.show()

    def add_marker(self, point):
        self.marker = L.marker(point['latlng'], options={"draggable": 'true'})
        self.marker.move.connect(self.move)  # new listener
        self.marker.moveend.connect(self.move_end)  # new listener
        self.map.addLayer(self.marker)

    def move(self, event):

        print('move', event)

    def move_end(self, event):

        print('Move end', event["sender"].latLng)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    widget = MapWindow()
    sys.exit(app.exec_())

@baspinarenes
Copy link
Author

Selection_023

@JaWeilBaum
Copy link

Currently you are overwriting the self.marker property when adding a new marker. I guess this causes the issues

@baspinarenes
Copy link
Author

Currently you are overwriting the self.marker property when adding a new marker. I guess this causes the issues

The same problem occurs when the marker is not object variable. I was thinking there was a problem converting the object from Python to Javascript. However, then there should have been a problem when adding manually. So strange.

@excalamus
Copy link

excalamus commented Mar 15, 2021

Selection_023

These errors aren't your fault. They're a problem with pyqtlet itself.

The second error, the existing clients won't be notified error, is because pyqtlet tries to register objects to the web channel after it has been initialized by the client. Python objects have to be registered to the Python QWebChannel prior to client initialization. I don't think any PyQt documentation exists for the QWebChannel; it only says TODO. However, the Qt for Python QWebChannel API says,

A current limitation is that objects must be registered before any client is initialized.

The first error is caused by the second. Since it begins with js:, the error originates from the Javascript interpreter. The object is created in pyqtlet's Evented._createJsObject . However, because the object isn't registered, JS doesn't have access to the Python object. The _createJsObject method therefore only creates a JS variable with the same name as the Python object. Hence, the JS object has no properties and the undefined errors occur when attempting to access them.

In order to access Python objects from the Javascript interpreter, objects must be registered before the client is initialized and the Python attribute must also be defined as a QtCore.Property, with a getter and setter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants