Skip to content

release 3.2: Qt6/QGIS 4 support and bug fixes#25

Open
fgianoli wants to merge 2 commits into
enricofer:masterfrom
fgianoli:qgis4
Open

release 3.2: Qt6/QGIS 4 support and bug fixes#25
fgianoli wants to merge 2 commits into
enricofer:masterfrom
fgianoli:qgis4

Conversation

@fgianoli

Copy link
Copy Markdown

release 3.2: Qt6/QGIS 4 support, native setDataSource, SQL view & provider fixes

  • Dual Qt5/Qt6 compatibility via qgis.PyQt + qualified enums (qgisMinimumVersion=3.20)
  • Replace XML-based datasource patching with native QgsMapLayer.setDataSource():
    preserves auxiliary storage (manual label positions, data-defined overrides)
    and clears layer tree warning/memory indicators after reload
  • Fix StopIteration on long PostGIS SQL views (WITH/window functions):
    memory layer fields now string(0) unlimited instead of string(250)
  • Add missing providers: mssql, hana, db2, wfs, arcgisfeatureserver (vector);
    wcs, arcgismapserver, xyz (raster)
  • Qt6 API fixes: fontMetrics().horizontalAdvance(), exec(), int coords in move()
  • Replace deprecated QgsMessageBar.CRITICAL with Qgis.MessageLevel.Critical
  • Use QgsWkbTypes.geometryDisplayString() for safer geometry type resolution
  • Null/None guards on locale, mapLayer lookups, expression results
  • Drop dead code (QgsMapLayerRegistry, qgisVersionOk), debug prints,
    Python 2 / PyQt4 shims, unused resources_rc import

@falkmielke

Copy link
Copy Markdown

The changeDataSource plugin is enormously useful for my work, and I much appreciate the update on this!

I could get the code of this PR to run on my machine; however, there was an issue with fontMetrics which I solved locally:

# (line 26)
from qgis.PyQt.QtGui import QIcon, QFontMetrics

# (line 309)
        idealWidth = QFontMetrics(QApplication.instance().font()).horizontalAdvance(txt) 

Here is the traceback:

AttributeError: 'QApplication' object has no attribute 'fontMetrics'
Traceback (most recent call last):
File "/QGIS/QGIS4/profiles/default/python/plugins/changeDataSource/changeDataSource.py", line 421, in run
self.populateLayerTable()
~~~~~~~~~~~~~~~~~~~~~~~^^
File "/QGIS/QGIS4/profiles/default/python/plugins/changeDataSource/changeDataSource.py", line 244, in populateLayerTable
self.dlg.layerTable.setCellWidget(lastRow, 0, self.getLabelWidget(layer.id(), 0, style=cellStyle))
~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/QGIS/QGIS4/profiles/default/python/plugins/changeDataSource/changeDataSource.py", line 309, in getLabelWidget
idealWidth = QApplication.instance().fontMetrics().horizontalAdvance(txt)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'QApplication' object has no attribute 'fontMetrics'


Python version: 3.14.5 (main, May 10 2026, 18:26:20) [GCC 16.1.1 20260430]
QGIS version: 4.0.2-Norrköping Norrköping, exported

Apart from this small change, the PR worked with the versions indicated above. 🥳

@falkmielke falkmielke left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(see comment on #25 ; these suggestions are the adjustments which worked for me to get the plugin running)

Comment thread changeDataSource.py Outdated
import os.path

from qgis.PyQt.QtCore import QSettings, QTranslator, QCoreApplication, Qt, pyqtSignal
from qgis.PyQt.QtGui import QIcon

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for fontMetrics to be used (below), I would suggest to add QFontMetrics:

from qgis.PyQt.QtGui import QIcon, QFontMetrics

Comment thread changeDataSource.py Outdated
edit = QLineEdit(parent = self.dlg.layerTable)
idealWidth = QApplication.instance().fontMetrics().width(txt)
edit = QLineEdit(parent=self.dlg.layerTable)
idealWidth = QApplication.instance().fontMetrics().horizontalAdvance(txt)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gives an error on the (almost) latest versions of QGIS and Python: fontMetrics have to be turned into an object QFontMetrics. The following worked for me:

        idealWidth = QFontMetrics(QApplication.instance().font()).horizontalAdvance(txt)

Comment thread changeDataSource.py Outdated
edit = QLineEdit(parent = self.dlg.layerTable)
idealWidth = QApplication.instance().fontMetrics().width(txt)
edit = QLineEdit(parent=self.dlg.layerTable)
idealWidth = QApplication.instance().fontMetrics().horizontalAdvance(txt)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
idealWidth = QApplication.instance().fontMetrics().horizontalAdvance(txt)
idealWidth = QFontMetrics(QApplication.instance().font()).horizontalAdvance(txt)

Comment thread changeDataSource.py Outdated
import os.path

from qgis.PyQt.QtCore import QSettings, QTranslator, QCoreApplication, Qt, pyqtSignal
from qgis.PyQt.QtGui import QIcon

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtGui import QIcon, QFontMetrics

@fgianoli

Copy link
Copy Markdown
Author

Thanks @falkmielke! Applied your QFontMetrics fix and bumped to 3.2.1 (commit a9dac49).

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

Successfully merging this pull request may close these issues.

2 participants