[Qgis-user] Toggle Attribute

L.W. eaglelw at gmx.de
Mon Nov 23 11:07:56 PST 2020


Implementation:

Starting position:

Two attribute tables with geodata and attributes, linked with each other
in the layer properties dialog.
When you click on an object in the map, the corresponding line to the
second object should be drawn.
If you click again, nothing more may be drawn.
If object B is set so that the lines should be displayed, then all
objects A should display the line regardless of a selection.

Symbol code:

Geometry generator -> LineString

if ("tab2_show_lines" = true OR is_selected () OR "tab1_hv",
make_line (make_point ("x1", "y1"), make_point ("x2", "y2")), '')



Action Script -> Layer Properties -> Actions -> Add New -> Python



Python code:

from qgis.utils import iface

layer_id = '[% @ layer_id%]'
layer_obj = QgsProject (). instance (). mapLayer (layer_id)

fid = [% $ id%]
hv = False if "[% hv%]" == 'false' else True

# QtWidgets.QMessageBox.information (None, "Clicked coords", "layer: [%
@layer_id%] \ ncoords: ([% @click_x%], [% @click_y%])")
# QtWidgets.QMessageBox.information (None, "fld", str (fld) + "" + str (hv))

layer_obj.startEditing ()
layer_obj.changeAttributeValue (fid, fld, not hv)
layer_obj.commitChanges ()



---



Cons / Problems / Known Limitations

Action:

Just click, no hover
Action can only be displayed via a combination button, not as a separate
button
Difficult to write action script (many clicks to get to the editor)
The layer with the Actionscript must be active / selected
You always have to select the action separately before clicking


Symbol:

Line not visible if the source object is not in the view (target object
does not matter), source / target connected by a link


Limitation:

My knowledge of python ;-)

---



Missing features

Edit project variables using the same action as written above



I hope you can learn a little from my gibberish.
Thank you all




Am 23.11.2020 um 15:24 schrieb Jorge Gustavo Rocha:
>
> Hi Mike,
>
> Just create an action with something like this to see it in action:
>
> https://www.google.pt/maps/@[% @click_x %],[% @click_y %]
>
> This will open google maps at the place you clicked. If you want to
> get something from the action called, you have to capture its output
> (and return the right value from your script).
>
> As Richard said, start with the default actions and you will see
> different kinds of things that can be done with actions. Capturing the
> clicked X,Y is straight forward.
>
> Jorge
>
> Às 13:22 de 23/11/20, Mike Flannigan escreveu:
>>
>> On 10/26/2014 I asked my first question in this forum:
>>
>> Coordinate Capture plugin allows one to click on the map, hit
>> the "Copy to clipboard" button, and then use the lat/long on
>> the clipboard. I want something similar, but I want to
>> skip the "Copy to clipboard" button. Ideally I want to
>> click on the map and it copies the lat/long to the clipboard,
>> executes an external script, and puts the result on the map
>> next to the place clicked. I can skip the last step if that
>> is too complicated.
>>
>> Can that be done with "Actions"? Or is the Python Console or
>> some other means a better way to get this done?
>>
>>
>>
>> It went unanswered and I have never found a way to execute a
>> script upon a click.  I've seen others who wanted actions based
>> on a click or hover, but no clear solutions presented.  It sounds
>> like you are on to something below.
>>
>>
>> Mike
>>
>>
>> On 11/23/20 5:24 AM, qgis-user-request at lists.osgeo.org wrote:
>>> I tried a little bit with "actions" but - I think you're right - there
>>> is no onHover.
>>>
>>> anyway, I found a much more comfortable solution for the moment.
>>>
>>> I add the simple term "OR is_selected()" in the if clause for my symbol
>>> to show the changed color (in my case I draw a line between two points)
>>> and it worked like a charme.
>>>
>>> But, this is without the option to e.g. display a messagebox or
>>> someting
>>> like that with additional informations of the feature (I know about map
>>> hints/tips (I do not know the right translation).
>>>
>>> Yes you could made an action for that, but my goal was to show this on
>>> hover, but click is oooookay.
>>>
>>> For X-Mas my wish in relation to QGIS is a hover event ... on
>>> feature on
>>> map and on symbols.;-)
>>>
>>> Thanks for helping to everyone.
>>>
>>
>>
>> _______________________________________________
>> Qgis-user mailing list
>> Qgis-user at lists.osgeo.org
>> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
>> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
> --
> Email Signature
> Logo <https://www.geomaster.pt>
> *Geomaster*
> *Jorge Gustavo Rocha* | Software Engineer
> *e:*jgr at geomaster.pt | *m:*+351 910 333 888
> *g:*41.54094,-8.40490 | *v: *510 906 109
> *a: * Rua António Cândido Pinto, 67, 4715-400 Braga
>
>
> _______________________________________________
> Qgis-user mailing list
> Qgis-user at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20201123/4680bdae/attachment.html>


More information about the Qgis-user mailing list