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

Custom Python function to access symbol color crashes QGIS #57807

Closed
1 of 2 tasks
thymaro opened this issue Jun 19, 2024 · 2 comments
Closed
1 of 2 tasks

Custom Python function to access symbol color crashes QGIS #57807

thymaro opened this issue Jun 19, 2024 · 2 comments
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter!

Comments

@thymaro
Copy link

thymaro commented Jun 19, 2024

What is the bug or the crash?

User Feedback

I entered a function in the function editor and loaded it:

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

@qgsfunction(args='auto', group='Custom')
def symbol_color(layername, feature, parent):
    layers = QgsProject.instance().mapLayersByName(layername)
    if not layers:
        return "Layer not found"
    
    layer = layers[0]
    renderer = layer.renderer()
    context = QgsRenderContext()
    symbol = renderer.symbolForFeature(feature, context)
    if symbol:
        values = symbol.color().getRgb()
        return "{}, {}, {}, {}".format(*values)
    else:
        return "Symbol not found"

then I went back to the expression builder and typed

symbol_color( @layer_name )

QGIS crashed before the last letter in '@layer_name' was even displayed.

Report Details

Python Stack Trace

Windows fatal exception: access violation

Current thread 0x00002670 (most recent call first):
  File "C:\OSGeo4W/apps/qgis-ltr/./python\qgis\core\additions\qgsfunction.py", line 74 in func
    return self.function(*values, **kwvalues)

Stack Trace


QgsExpression::evaluate :
QgsCategorizedSymbolRenderer::valueForFeature :
QgsCategorizedSymbolRenderer::originalSymbolForFeature :
pdal::PointView::table :
PyLong_FromString :
PyObject_Vectorcall :
PyObject_Vectorcall :
PyEval_EvalFrameDefault :
PyFunction_Vectorcall :
PyArg_CheckPositional :
PyObject_Call :
PyObject_Call :
PyInit_sip :
PyInit_sip :
pdal::StreamPointTable::reset :
pdal::MetadataNode::value :
QgsExpressionFunction::run :
pdal::MetadataNode::value :
QgsExpressionNodeFunction::evalNode :
QgsExpressionNode::eval :
QgsExpression::evaluate :
QgsExpressionPreviewWidget::refreshPreview :
QgsExpressionBuilderWidget::txtExpressionString_textChanged :
QObject::qt_static_metacall :
QsciScintilla::handleModified :
QsciScintilla::qt_static_metacall :
QObject::qt_static_metacall :
QsciScintillaBase::SCN_MODIFIED :
QsciScintillaBase::inputMethodQuery :
QsciScintillaBase::inputMethodQuery :
QsciScintillaBase::inputMethodQuery :
QsciScintillaBase::inputMethodQuery :
QsciScintillaBase::inputMethodQuery :
QsciScintillaBase::inputMethodQuery :
QsciScintillaBase::inputMethodQuery :
QsciScintillaBase::inputMethodQuery :
QsciScintillaBase::inputMethodQuery :
QsciScintillaBase::inputMethodQuery :
QsciScintillaBase::keyPressEvent :
QgsCodeEditor::keyPressEvent :
QWidget::event :
QFrame::event :
QAbstractScrollArea::event :
QApplicationPrivate::notify_helper :
QApplication::notify :
QgsApplication::notify :
QCoreApplication::notifyInternal2 :
QSizePolicy::QSizePolicy :
QApplicationPrivate::notify_helper :
QApplication::notify :
QgsApplication::notify :
QCoreApplication::notifyInternal2 :
QGuiApplicationPrivate::processKeyEvent :
QWindowSystemInterface::sendWindowSystemEvents :
QEventDispatcherWin32::processEvents :
qt_plugin_query_metadata :
QEventLoop::exec :
QDialog::exec :
QgsPropertyOverrideButton::showExpressionDialog :
QObject::qt_static_metacall :
QMenu::~QMenu :
QObject::qt_static_metacall :
QAction::activate :
QMenu::actionGeometry :
QMenu::actionGeometry :
QMenu::mouseReleaseEvent :
QWidget::event :
QMenu::event :
QApplicationPrivate::notify_helper :
QApplication::notify :
QgsApplication::notify :
QCoreApplication::notifyInternal2 :
QApplicationPrivate::sendMouseEvent :
QSizePolicy::QSizePolicy :
QSizePolicy::QSizePolicy :
QApplicationPrivate::notify_helper :
QApplication::notify :
QgsApplication::notify :
QCoreApplication::notifyInternal2 :
QGuiApplicationPrivate::processMouseEvent :
QWindowSystemInterface::sendWindowSystemEvents :
QEventDispatcherWin32::processEvents :
qt_plugin_query_metadata :
QEventLoop::exec :
QMenu::exec :
QToolButton::paintEvent :
QToolButton::showMenu :
QObject::qt_static_metacall :
QAbstractButton::clicked :
QAbstractButton::mousePressEvent :
QToolButton::mousePressEvent :
QWidget::event :
QApplicationPrivate::notify_helper :
QApplication::notify :
QgsApplication::notify :
QCoreApplication::notifyInternal2 :
QApplicationPrivate::sendMouseEvent :
QSizePolicy::QSizePolicy :
QSizePolicy::QSizePolicy :
QApplicationPrivate::notify_helper :
QApplication::notify :
QgsApplication::notify :
QCoreApplication::notifyInternal2 :
QGuiApplicationPrivate::processMouseEvent :
QWindowSystemInterface::sendWindowSystemEvents :
QEventDispatcherWin32::processEvents :
qt_plugin_query_metadata :
QEventLoop::exec :
QCoreApplication::exec :
main :
BaseThreadInitThunk :
RtlUserThreadStart :

QGIS Info
QGIS Version: 3.34.7-Prizren
QGIS code revision: 6f7d735
Compiled against Qt: 5.15.13
Running against Qt: 5.15.13
Compiled against GDAL: 3.9.0
Running against GDAL: 3.9.0

System Info
CPU Type: x86_64
Kernel Type: winnt
Kernel Version: 10.0.22631

Steps to reproduce the issue

  1. choose categorized layer styling
  2. click the general symbol
  3. Edit layer stroke color as data defined override
  4. add custom python function
  5. go back to expression builder and enter new function wrapping '@layer_name' (without the apostrophes)
  6. watch it burn

Versions

<style type="text/css"> p, li { white-space: pre-wrap; } </style>
QGIS version 3.34.7-Prizren QGIS code revision 6f7d735
Qt version 5.15.13
Python version 3.12.3
GDAL/OGR version 3.9.0
PROJ version 9.4.0
EPSG Registry database version v11.004 (2024-02-24)
GEOS version 3.12.1-CAPI-1.18.1
SQLite version 3.45.1
PDAL version 2.6.3
PostgreSQL client version 16.2
SpatiaLite version 5.1.0
QWT version 6.2.0
QScintilla2 version 2.14.1
OS version Windows 11 Version 2009
       
Active Python plugins
copy_coords 0.3
mask 1.10.2
menu_from_project 2.1.0
mmqgis 2021.9.10
processing_r 4.1.0
processing_saga_nextgen 1.0.0
profiletool 4.2.6
qfieldsync v4.9.1
QuickOSM 2.2.3
quick_layer 0.3
quick_map_services 0.19.34
rectanglify 1.0.3
splitmultipart 1.0.0
SplitPolygonShowingAreas 0.13
swissgeodownloader 2.0.1
swiss_locator 3.0.0
VoGisProfilTool 3.0.2
db_manager 0.1.20
grassprovider 2.12.99
processing 2.12.99
QGIS version 3.34.7-Prizren QGIS code revision [6f7d735](https://github.com/qgis/QGIS/commit/6f7d735c) Qt version 5.15.13 Python version 3.12.3 GDAL/OGR version 3.9.0 PROJ version 9.4.0 EPSG Registry database version v11.004 (2024-02-24) GEOS version 3.12.1-CAPI-1.18.1 SQLite version 3.45.1 PDAL version 2.6.3 PostgreSQL client version 16.2 SpatiaLite version 5.1.0 QWT version 6.2.0 QScintilla2 version 2.14.1 OS version Windows 11 Version 2009

Active Python plugins
copy_coords
0.3
mask
1.10.2
menu_from_project
2.1.0
mmqgis
2021.9.10
processing_r
4.1.0
processing_saga_nextgen
1.0.0
profiletool
4.2.6
qfieldsync
v4.9.1
QuickOSM
2.2.3
quick_layer
0.3
quick_map_services
0.19.34
rectanglify
1.0.3
splitmultipart
1.0.0
SplitPolygonShowingAreas
0.13
swissgeodownloader
2.0.1
swiss_locator
3.0.0
VoGisProfilTool
3.0.2
db_manager
0.1.20
grassprovider
2.12.99
processing
2.12.99

Supported QGIS version

  • I'm running a supported QGIS version according to the roadmap.

New profile

Additional context

No response

@thymaro thymaro added the Bug Either a bug report, or a bug fix. Let's hope for the latter! label Jun 19, 2024
@nyalldawson
Copy link
Collaborator

This isn't a qgis bug -- you're not using the API correctly. Please use the mailing lists or stackexchange for user support.

@thymaro
Copy link
Author

thymaro commented Jun 21, 2024

Thanks, Nyall. At least I know what the problem could be, then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter!
Projects
None yet
Development

No branches or pull requests

2 participants