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

QgsFields are viewed as the same despite having different QgsEditorWidgetSetup's #55873

Closed
2 tasks done
jakimowb opened this issue Jan 17, 2024 · 0 comments · Fixed by #56264
Closed
2 tasks done

QgsFields are viewed as the same despite having different QgsEditorWidgetSetup's #55873

jakimowb opened this issue Jan 17, 2024 · 0 comments · Fixed by #56264
Assignees
Labels
API API improvement only, no visible user interface changes Bug Either a bug report, or a bug fix. Let's hope for the latter!

Comments

@jakimowb
Copy link
Contributor

What is the bug or the crash?

The QgsField operator== does not recognize changes of the editor widget setup type. As a consequence, a call QgsVectorLayer::setEditorWidgetSetup(...), which calls QgsVectorLayer::updateFields(), will not recognize the change of field attributes and not trigger the QgsVectorLayer::updatedFields() signal.

  // qgsvectorlayer.cpp lines 4511 to 4517
  if ( oldFields != mFields )
  {
    emit updatedFields();
    mEditFormConfig.setFields( mFields );
  }

According to the docs, the QgsVectorLayer::updatedFields() signal should be emitted whenever the fields available from this layer have been changed. As this is not the case, objects / widgets that rely on the signal and use QgsField information may have problems to get refreshed accordingly.

As a workaround, a call to QgsVectorLayer::setEditorWidgetSetup(...) may explicitely followed by an emit of updatedFields, like in this python snippet:

layer: QgsVectorLayer
layer.setEditorWidgetSetup(0, QgsEditorWidgetSetup('Color', {}))
layer.updateFields.emit()

Steps to reproduce the issue

The following python code that two fields are considered equal despite having different

from qgis.PyQt.QtCore import QVariant
from qgis._core import QgsVectorLayer
from qgis.core import QgsField, QgsEditorWidgetSetup, edit

from qgis.gui import QgsGui
from qgis.testing import start_app

start_app()

if len(QgsGui.editorWidgetRegistry().factories()) == 0:
    QgsGui.editorWidgetRegistry().initEditors()

# Example 1: change editor widget setup

def onFieldsUpdated():
    print('-- updatedFields emitted!')

uri = "point?crs=epsg:4326&field=color:string"
layer = QgsVectorLayer(uri, "Scratch point layer",  "memory")
layer.updatedFields.connect(onFieldsUpdated)

# Example 1: updatesFields is not emitted when changing the editorWidgetSetup
with edit(layer):
    # this emits the updatedFields signal
    print('Add QgsField')
    layer.addAttribute(QgsField('info', QVariant.String))

# this does not emit the updatedFields signal
print('Change QgsField editorWidgetSetup (no emit of updatedFields)')
layer.setEditorWidgetSetup(0, QgsEditorWidgetSetup('Color', {}))

# Example 2: editorWidgetSetup and comment not considered in field comparison
field1 = QgsField('info', QVariant.String)
field2 = QgsField('info', QVariant.String)
assert field1 == field2
assert field1.editorWidgetSetup().type() == field2.editorWidgetSetup().type()
assert field1.comment() == field2.comment()

# change comment and editorWidgetSetup
field2.setEditorWidgetSetup(QgsEditorWidgetSetup('Color', {}))
field2.setComment('Color info value')

assert field1.editorWidgetSetup().type() != field2.editorWidgetSetup().type()
assert field1.comment() != field2.comment()

# different QgsEditorWidgetSetup types, but still same field?
assert field1 != field2

Versions

<style type="text/css"> p, li { white-space: pre-wrap; } </style>
QGIS version 3.34.2-Prizren QGIS code revision 7d19979
Qt version 5.15.3
Python version 3.9.5
Compiled against GDAL/OGR 3.8.2 Running against GDAL/OGR 3.8.3
PROJ version 9.3.1
EPSG Registry database version v10.098 (2023-11-24)
GEOS version 3.12.1-CAPI-1.18.1
SQLite version 3.41.1
PDAL version 2.6.0
PostgreSQL client version 15.2
SpatiaLite version 5.1.0
QWT version 6.1.6
QScintilla2 version 2.13.4
OS version Windows 10 Version 2009
       
Active Python plugins
enmapboxplugin 3.13.2
plugin_reloader 0.9.3
pyqgis_resource_browser 0.3.0
quick_map_services 0.19.34
db_manager 0.1.20
grassprovider 2.12.99
MetaSearch 0.3.6
processing 2.12.99
QGIS version 3.34.2-Prizren QGIS code revision [7d19979](https://github.com/qgis/QGIS/commit/7d199797fc) Qt version 5.15.3 Python version 3.9.5 Compiled against GDAL/OGR 3.8.2 Running against GDAL/OGR 3.8.3 PROJ version 9.3.1 EPSG Registry database version v10.098 (2023-11-24) GEOS version 3.12.1-CAPI-1.18.1 SQLite version 3.41.1 PDAL version 2.6.0 PostgreSQL client version 15.2 SpatiaLite version 5.1.0 QWT version 6.1.6 QScintilla2 version 2.13.4 OS version Windows 10 Version 2009

Supported QGIS version

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

New profile

Additional context

No response

@jakimowb jakimowb added the Bug Either a bug report, or a bug fix. Let's hope for the latter! label Jan 17, 2024
jakimowb added a commit to EnMAP-Box/qgispluginsupport that referenced this issue Jan 19, 2024
Signed-off-by: Benjamin Jakimow benjamin.jakimow@geo.hu-berlin.de <benjamin.jakimow@geo.hu-berlin.de>
@elpaso elpaso self-assigned this Feb 8, 2024
@elpaso elpaso added the API API improvement only, no visible user interface changes label Feb 8, 2024
elpaso added a commit to elpaso/QGIS that referenced this issue Feb 8, 2024
elpaso added a commit to elpaso/QGIS that referenced this issue Feb 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API API improvement only, no visible user interface changes Bug Either a bug report, or a bug fix. Let's hope for the latter!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants