8

I have a layer in my system that displays nautical sectorlights. If sectorlights are close together the symbology overlaps, wрich I don't want.

I would like to see the star at all times, but see the dotted lines, colors and arc-indicator when the layer is active and I hold my mouse on the star.

Are mouseover actions possible in QGIS? Can a mouseover action be invoked as a parameter in an Expression Builder?

For example:

Enable layer:

if(
[mouseover]=true, 1, 0)

At the moment I am using a layer without geometry as a reference layer. I keep the attribute table open while working and if i check the box on "sectorlichten-uitgebreid" the symbology will show on screen. I had hoped to evolve to a mouse-over action, my thoughts: There is mouse-over functionality being used on maptips and, in a way, the snapping-function is doing what I am looking for: when you come within a set px of a point, a purple box (symbology) will appear, indicating you are snapping to a geometry. I had hoped this signal was available to invoke functionality in the expression-builder.

enter image description here enter image description here

2
  • 3
    If you can't find a way to detect a mouseover, the is_selected function would make a crude workaround.
    – csk
    Commented Nov 21, 2019 at 15:53
  • I don't see any existing function to detect a mouseover or cursor location. You might need to write a custom function. That's probably pretty simple if you know python. If not, you'll run into the GIS stackexchange catch-22, where you can't ask about coding if you don't already have some code.
    – csk
    Commented Nov 21, 2019 at 16:06

3 Answers 3

5

This is a workaround, not a direct answer. To achieve what you actually wanted, with symbols only appearing when you mouse over the feature, you could probably write a custom function.

The is_selected function is the closest to a [mouseover] function in the existing Expression Builder Functions. Here's how to use it to control symbology:

Here's what my layer looks like before:

enter image description here

  1. In the Layer styling panel, select the symbol layer that has the dotted lines, colors and arc-indicator.
  2. Click on the data-defined override button next to the words "enable layer" at the bottom of the panel (you may need to scroll down).
  3. Enter the expression is_selected().

    enter image description here

    Here's what it looks like now, with one point selected:

    enter image description here

    To activate the symbol layer, you'll have to select the feature (as opposed to just mousing over it like you wanted). When the feature is selected, it becomes whatever color is set for selected features. By default, that color is opaque yellow. Now instead of overlapping symbols, you have one symbol that completely obscures its neighbors. Obviously that's not ideal.

  4. Go into project properties. Change the selection color to transparent. Now the selected feature shows the symbology you want.

    enter image description here

Note: changes to the feature selection color will apply to all the layers in the project. You might want to choose a semi-transparent selection color instead of fully transparent, so you can still tell when features in other are selected. Here's what my layer looks like with 30% opacity:

enter image description here

0
3

This post builds upon @csk's answer where you can create a custom function to use in conjunction with Map tips. This will let you hover over a feature and see only that feature's symbology.

After you have used the is_selected() expression for each of your symbol layers (excluding the star), right-click your layer and go to Properties > Display:

Maptip interface

Click the epsilon button highlighted in the blue circle. Now we can define a new function by either editing an existing file or creating a new one. Then use the following code, change to match the name of your layer:

from qgis.core import *
from qgis.gui import *

canvas = qgis.utils.iface.mapCanvas()
layer = QgsProject.instance().mapLayersByName("Point example")[0]

def hide():
    layer.selectByIds([])
    canvas.xyCoordinates.disconnect(hide)

@qgsfunction(args='auto', group='Custom')
def show(feature, parent):
    single_symbol_renderer = layer.renderer()
    symbol = single_symbol_renderer.symbol()
    for x in symbol.symbolLayers()[:-1]:
        if feature:
            layer.selectByIds([feature.id()])
    canvas.xyCoordinates.connect(hide)

Creating function

What the code does is when Map tips is enabled from the toolbar, it checks to see where the cursor is. If it is hovering over a feature, it will select it automatically. A second function is connected to a signal which is emited when the xy coordinate changes on canvas (i.e. when the cursor moves). This function then deselects all features, thus hiding the symbology again.

Now click the Save and Load Functions button. Then set the function using the expression show() and click the Insert button or type it in directly using [% show() %].

Set expression

Click Apply and OK. Now when you hover over your feature, the relevant symbology should be applied.


Result

0
2

Maybe you can do it without programming much by using the Map Tips or HTML Map Tips tool:

enter image description here

HTML Map Tips can be styled more sensibly, but I am not sure about the rotation you need to apply, maybe an svg transform statement?

There is an old but nice tutorial here.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.