QGIS icon indicating copy to clipboard operation
QGIS copied to clipboard

Fix pasting unsaved changes as temporary scratch layers

Open uclaros opened this issue 1 year ago • 6 comments

Description

Pasting unsaved changes as a temporary scratch layer could fail when source is a db. Geopackages would have Autogenerate as an fid value, Postgres layers would have nextval(... values etc, which could not be stored in the target int field.

Instead of failing, this PR continues with a null value instead.

Fixes #38913

uclaros avatar Jul 04 '24 13:07 uclaros

🪟 Windows builds ready!

Windows builds of this PR are available for testing here. Debug symbols for this build are available here.

(Built from commit 1a0d81d767d42d39921d955bdcb16b2f36866d4e)

github-actions[bot] avatar Jul 04 '24 13:07 github-actions[bot]

Does this work if you modify QgsField::convertCompatible to add something like this just after the initial null check?

  if ( QgsVariantUtils::isNull( v ) )
  {
    v.convert( d->type );
    return true;
  }

  if ( v.userType() == QMetaType::type( "QgsUnsetAttributeValue" ) )
  {
    return true;
  }

nyalldawson avatar Jul 05 '24 03:07 nyalldawson

Autogenerate and nextval(...) are of userType() == QMetaType::Type::QString, so that will not work.

uclaros avatar Jul 05 '24 06:07 uclaros

@uclaros

Autogenerate and nextval(...) are of userType() == QMetaType::Type::QString,

They shouldn't be! Are you checking the field type or value type?

nyalldawson avatar Jul 05 '24 07:07 nyalldawson

Are you checking the field type or value type?

Value type is string, field type should be int.

I can see QgsUnsetAttributeValue is only used when splitting and duplicating features. It's not clear to me how this class should be used.

uclaros avatar Jul 05 '24 07:07 uclaros

@nyalldawson when convertCompatible() fails, it updates the value to the appropriate null QVariant and returns false. In this case of pasting to the memory provider, we don't really care about the return value: even if the Autogenerate/nextval was of metatype QgsUnsetAttributeValue and it was handled properly as you suggest, it should still update the value to the appropriate null QVariant or it would fail to be stored on the int field. Hence my suggestion to completely skip the return value check.

uclaros avatar Aug 12 '24 07:08 uclaros

🪟 Windows builds

Download Windows builds of this PR for testing. Debug symbols for this build are available here. (Built from commit 1a0d81d767d42d39921d955bdcb16b2f36866d4e)

🪟 Windows Qt6 builds

Download Windows Qt6 builds of this PR for testing. (Built from commit 1a0d81d767d42d39921d955bdcb16b2f36866d4e)

github-actions[bot] avatar Jan 03 '25 07:01 github-actions[bot]

Merging as a temporary fix (see comments in https://github.com/qgis/QGIS/pull/60474)

nyalldawson avatar Feb 07 '25 00:02 nyalldawson